Ownership is Rust's contract for memory safety. Every value has exactly one owner; when the owner goes out of scope, Rust calls drop automatically.
The three rules
- Each value has one owner at a time
- Only one owner—when the owner scope ends, the value is dropped
- While borrowed, the owner cannot move or mutate in conflicting ways
Scope and drop
{ let s = String::from("hi"); } // s dropped here
Important interview questions and answers
- Q: Who frees heap memory for a String?
A: Rust when the owning variable goes out of scope—no manual free in safe code. - Q: Can two variables own the same String?
A: Not simultaneously—ownership moves or is shared via borrowing/reference counting patterns.
Self-check
- When is memory for a heap value freed?
- How many owners at once?
Pitfall: Using a variable after it was moved causes compile errors—compare with Java where references are copied freely.
Interview prep
- What problem does ownership solve?
Prevents double-free, use-after-free, and data races in safe code without runtime GC overhead.