Rust Ownership: The Mental Model That Makes It Click
Ownership is Rust's most distinctive feature. Once you build the right mental model, it becomes intuitive.
Key Insights
- Think of ownership as a compile-time garbage collector that runs at zero cost
- Most ownership errors come from trying to use values after moving them
- Clone is not a code smell — sometimes explicit copies are the right choice
The Core Rules
- Each value has exactly one owner
- When the owner goes out of scope, the value is dropped
- You can transfer ownership (move) or borrow (reference)
fn main() {
let name = String::from("hello");
let greeting = format!("Hi, {name}"); // name is borrowed here, not moved
println!("{greeting}");
println!("{name}"); // still valid
}
When to Clone
If you’re fighting the borrow checker and the data is small, just clone it:
let config = load_config();
let handler = move || {
process(config.clone()) // explicit copy, no lifetime issues
};
References vs Ownership
Use &T when you just need to read. Use &mut T when you need to modify. Take ownership (T) only when you need to store or consume the value.