Rust Fundamentals interview question · Most asked
Explain ownership in Rust.
Summary
Every value has a single owner; when the owner goes out of scope the value is dropped. Assigning or passing a non-Copy value moves ownership, so the old binding can't be used, which prevents double frees and use-after-free without a garbage collector.
Longer Answer
Copy types (integers, floats, bool, char) are duplicated instead of moved. clone() makes explicit deep copies. Borrowing (&T, &mut T) lets code use a value without taking ownership.
Key Points
- One owner; dropped at end of scope.
- Moves for heap types; Copy for simple ones.
- Borrowing avoids moves.
Deep dive: Rust Fundamentals: Ownership