Theme
Q014. Can a moved binding be reinitialized?
Short Answer
Yes. A moved-from place is uninitialized, not permanently invalid. If its binding is mutable, assigning a new value initializes it again and makes it usable. The new value is unrelated to the moved value, which remains owned by its destination. Rust tracks both values independently and drops each through its current owning path.
Deep Dive
Reinitialization writes a new valid value into the old place:
text
source = initialized
move source to destination
source = moved from
assign new value to source
source = initialized againThe binding generally needs mut because it is assigned after its initial initialization. Once reinitialized, it can be read, borrowed, moved, or dropped normally.
Reinitialization does not undo a move. The destination still owns the original value, while the source owns the newly assigned value.
Internal Model
Move analysis tracks initialization state at each program point. Assignment to a moved-from place changes that state back to initialized without dropping an old value there, because the old value is no longer present semantically.
At scope exit, drop glue runs for whichever places are initialized along that control-flow path.
Example
rust
fn main() {
let mut source = String::from("first");
let destination = source;
source = String::from("second");
println!("destination: {destination}");
println!("source: {source}");
}Common Mistakes
- Thinking reinitialization transfers the original value back.
- Expecting assignment into a moved-from place to drop the moved value.
- Forgetting that a binding assigned again generally needs to be mutable.
Follow-ups
- How does reinitialization differ from shadowing?
- How does conditional reinitialization affect later use?
- Which value is dropped first after reinitialization?
Related
- Q010. How does ownership determine a value's lifetime?
- Q013. What happens to the source binding after a move?
- Q015. How do reassignment and shadowing differ from a move?
References
- The Rust Programming Language
- Rust Reference