我有一个带字段的 struct :
struct A {
field: SomeType,
}
给定一个&mut A
,我如何移动field
的值并交换一个新值?
fn foo(a: &mut A) {
let mut my_local_var = a.field;
a.field = SomeType::new();
// ...
// do things with my_local_var
// some operations may modify the NEW field's value as well.
}
最终目标相当于get_and_set()
次手术.我不担心这种情况下的并发性.