I have a struct with a field:
struct A { field: SomeType, }
Given a &mut A, how can I move the value of field
&mut A
field
Use std::mem::swap().
fn foo(a: &mut A) { let mut my_local_var = SomeType::new(); mem::swap(&mut a.field, &mut my_local_var); }
Or std::mem::replace().
fn foo(a: &mut A) { let mut my_local_var = mem::replace(&mut a.field, SomeType::new()); }