mem::take
and mem::replace
are both used to manipulate ownership and values, but they work slightly differently.
mem::take
The mem::take method takes ownership of a value and replaces it with a default value of the same type. The original value is returned.
Her is a simple example:
#![allow(unused)]
fn main() {
use std::mem;
let mut v: Vec<i32> = vec![1, 2];
let old_v = mem::take(&mut v);
println!("Old value: {:?}", old_v); // Old value: [1, 2]
println!("New value: {:?}", v); // New value: []
}
In the above example, mem::take
is used to take ownership of v
and replace it with an empty vector and then returning the new value which is now a default value, in our case an empty vector.
A typical use case for this function is when you are working on data structures like vectors or hashmap that you’ll like to empty their values and assign new values to them for example, we can now assign new values to v
with .push
method like so:
#![allow(unused)]
fn main() {
use std::mem;
let mut v: Vec<i32> = vec![1, 2];
let old_v = mem::take(&mut v);
v.push(8);
println!("Old value: {:?}", old_v); [1,2]
println!("New value: {:?}", v); // [8]
}
mem::replace
mem::replace
also takes ownership of a value and replaces it with a new value of the same type.
#![allow(unused)]
fn main() {
use std::mem;
let mut v: Vec<i32> = vec![1, 2];
let old_v = mem::replace(&mut v, vec![3, 4, 5]);
println!("Old value: {:?}", old_v); // [1,2]
println!("New value: {:?}", v); // [3, 4, 5]
}
In the above code, we’ve replace the content of v
[1,2]
with a new vector [3,4,5]
so, v now contains [3,4,5]
and the new owner of the content of v
can do whatever it likes with v
‘s content.
This is a complete overhaul and replacement of content. This can be useful for state management related applications where you’ll need to update the state with a completely new information.
Happy hacking!