Assigning a non-Copy value moves ownership—the source becomes invalid. Borrowing lets you use data without taking ownership via references.
Move example
let a = String::from("x");
let b = a; // a moved; println!("{}", a) errors
Borrowing preview
let s = String::from("hello");
let len = s.len(); // s still valid—no move
Important interview questions and answers
- Q: Move vs shallow copy?
A: Move invalidates the source; Copy types duplicate bits without invalidating. - Q: Why moves exist?
A: Prevent double-free and use-after-free without GC.
Self-check
- What happens to the old variable after a move?
- Does calling
.len()move a String?
Tip: If you need to keep using a String, borrow with &s or call .clone() explicitly—moves are intentional, not bugs.
Interview prep
- Move vs Copy?
Copy types duplicate bits on assignment (e.g.
i32); move types transfer ownership and invalidate the source (e.g.String).