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);
}
fn foo(a: &mut A) {
let mut my_local_var = mem::replace(&mut a.field, SomeType::new());
}
More Related Contents:
- Return local String as a slice (&str)
- What is the difference between iter and into_iter?
- Why is it legal to borrow a temporary?
- Package with both a library and a binary?
- How can I create my own data structure with an iterator that returns mutable references?
- Do mutable references have move semantics?
- What are the options to end a mutable borrow in Rust?
- How do I match enum values with an integer?
- How does Rust provide move semantics?
- How do I convert a Rust closure to a C-style callback?
- How to transform fields during serialization using Serde?
- “borrowed value does not live long enough” when using the builder pattern
- cannot borrow `self.x` as immutable because `*self` is also borrowed as mutable
- Using the same iterator multiple times in Rust
- How do I fix “cannot find derive macro in this scope”?
- Thread ” has overflowed its stack when allocating a large array using Box
- Why is the bound `T: ‘a` required in order to store a reference `&’a T`?
- How do you enable a Rust “crate feature”?
- How to implement Iterator yielding mutable references [duplicate]
- How can a Rust program access metadata from its Cargo package?
- How can the ref keyword be avoided when pattern matching in a function taking &self or &mut self?
- How do I disambiguate traits in Rust?
- How to swap two variables?
- Expected bound lifetime parameter, found concrete lifetime [E0271]
- How do I change a function’s qualifiers via conditional compilation?
- Creating a vector of zeros for a specific size
- What is the difference between immutable and const variables in Rust?
- How do I create a HashMap with type erased keys?
- How to print structs and arrays?
- Can Rust optimise away the bit-wise copy during move of an object someday?