Ownership
Try it yourself →Every value has a single owner.
Syntax
let s2 = s1; // s1 moved
Description
If you assign a value like a String to another variable, ownership is MOVED; the old variable can no longer be used. This prevents memory errors at compile time.
Example
fn main() {
let s1 = String::from("Alex");
let s2 = s1;
println!("{}", s2);
}Expected output
Alex
When to use?
Memory safety (without a garbage collector).
Common mistakes
- ⚠️ Using the old variable after a move is an ERROR: s1 is now invalid.