What is ownership in Rust and what are its three rules?
Understand Rust ownership and its three rules — one owner, one at a time, dropped at scope end — and how move semantics prevent memory bugs, with code examples.
Expected Interview Answer
Ownership is Rust's compile-time memory-management system in which every value has exactly one owning variable, and the value is automatically freed when that owner goes out of scope — giving safety without a garbage collector.
The three rules are: (1) each value has a single owner; (2) there can only be one owner at a time; and (3) when the owner goes out of scope, the value is dropped and its memory released. Assigning or passing a heap value moves ownership, invalidating the original binding so it can no longer be used, which prevents double frees and use-after-free. Types that are cheap to duplicate implement Copy and are copied instead of moved.
- Deterministic, automatic cleanup with no garbage collector
- Prevents double-free and use-after-free at compile time
- Makes resource lifetimes explicit and predictable
- Enables safe concurrency by controlling aliasing
- No runtime overhead for memory management
AI Mentor Explanation
Ownership is like the single match ball in play: only one fielder holds it at a time, and when they hand it off, they no longer control it. There is never a moment where two players legitimately possess the same ball. When the over ends and the ball leaves the field of play, it is cleanly retired — just as a value is dropped when its owner's scope ends.
Step-by-Step Explanation
Step 1
Rule one: single owner
Every value is bound to exactly one variable, which is its owner responsible for the value's memory.
Step 2
Rule two: one owner at a time
Assigning or passing a heap value moves ownership; the previous binding becomes invalid and cannot be used.
Step 3
Rule three: drop on scope end
When the owner goes out of scope, Rust calls drop automatically, freeing the value's resources deterministically.
Step 4
Understand move vs copy
Heap types move by default; simple stack types that implement Copy are duplicated instead of moved.
Step 5
Transfer explicitly
Use clone for a deep copy, or borrow with references when you need access without taking ownership.
What Interviewer Expects
- Stating all three ownership rules accurately
- Explaining move semantics and invalidation of the old binding
- Distinguishing Copy types from move-only types
- Understanding that drop runs automatically at scope end
- Relating ownership to preventing double-free bugs
Common Mistakes
- Saying a value can have multiple owners simultaneously
- Thinking a moved variable is still usable
- Confusing move with a deep clone
- Forgetting that scope end triggers automatic drop
Best Answer (HR Friendly)
“Ownership is Rust's way of tracking who is responsible for a piece of data so it gets cleaned up exactly once, automatically. Each value has one owner, only one at a time, and when that owner is done the data is freed — which is how Rust avoids memory bugs without a garbage collector.”
Code Example
fn main() {
let a = String::from("data");
let b = a; // ownership moves from a to b
// println!("{}", a); // error: value moved
println!("{}", b); // b is the sole owner
} // b goes out of scope here -> String is dropped and freedFollow-up Questions
- What is the difference between a move and a Copy in Rust?
- How does ownership prevent double-free errors?
- What happens when you pass an owned value into a function?
- How do references let you access data without taking ownership?
MCQ Practice
1. How many owners can a Rust value have at one time?
Rule two of ownership states there can be only one owner at a time; assigning the value moves ownership and invalidates the old binding.
2. What happens when a value's owner goes out of scope?
The third rule: when the owner leaves scope, Rust calls drop automatically, releasing the value's resources deterministically.
Flash Cards
State Rust's three ownership rules. — 1) Each value has one owner. 2) Only one owner at a time. 3) When the owner leaves scope, the value is dropped.
What is a move? — Transferring ownership of a heap value to a new binding, which invalidates the original so it can no longer be used.
Move vs Copy? — Heap types move by default; small stack types implementing Copy are bitwise-duplicated, leaving the original usable.
How is memory freed? — Automatically and deterministically via drop when the owner goes out of scope — no garbage collector.