Ownership, Moves, and Safe Borrowing
What are ownership and borrowing in Rust, and how do they keep memory access safe?
what they're testing: The interviewer is checking whether you can reason about who controls a value, when that control moves, and which aliases Rust permits.
Ownership says which binding is responsible for a value. Each value has one owner, and Rust drops the value when its owner goes out of scope. Assigning or passing a non-Copy value normally moves it, so the old binding can no longer be used. A Copy value, such as an integer, is duplicated implicitly and leaves the source usable.
Borrowing creates a reference without taking ownership. A shared reference, &T, does not normally allow mutation through that reference. Interior-mutability types are an explicit exception. A mutable reference, &mut T, grants exclusive access and permits mutation. The referenced value is not dropped when the reference stops being used, and the compiler rejects a reference that could outlive the value. For the same data, Rust allows any number of shared references or one mutable reference at a time. A borrow can end after its final use instead of lasting to the closing brace.
Together, these rules provide predictable cleanup and let safe Rust prevent dangling references and data races without a garbage collector. Function signatures show the ownership choice: a non-Copy T transfers ownership into the function, &T borrows shared access, and &mut T borrows mutable access.
Where people slip
the tempting wrong answer, and what's actually true
Assigning a `String` to another variable makes a deep copy, so both variables remain usable.
The assignment moves the `String` and invalidates the old binding unless the value is explicitly cloned.
Borrowing transfers ownership temporarily and gives it back when the reference goes out of scope.
A borrow creates a reference without transferring ownership, so the original owner remains responsible for the value.
Rust allows only one reference to a value at a time.
For the same data, Rust allows any number of shared references or one mutable reference at a time.
A borrow always lasts until the closing brace of its lexical scope.
A borrow can end after its final use, allowing a later non-overlapping borrow in the same block.
If they push further
What is the difference between a move and a copy?
A move makes the source binding unusable, while a `Copy` type is duplicated implicitly and leaves the source usable. Both may copy bits internally; the key difference is the language-level validity of the source.
Why can Rust have many immutable borrows but only one mutable borrow?
Ordinary shared borrows do not grant mutation, so they can coexist. A mutable borrow needs exclusive access so reads and writes cannot overlap unsafely; interior-mutability types provide controlled mutation through shared references.
How do lifetimes relate to borrowing?
Lifetimes describe how long references are valid, letting the compiler reject any reference that could outlive the data it borrows.
Sources
- The Rust Programming Language: What Is Ownership? ↗doc.rust-lang.org
- The Rust Programming Language: References and Borrowing ↗doc.rust-lang.org
- Rust Standard Library: Copy ↗doc.rust-lang.org
- The Rust Reference: Interior Mutability ↗doc.rust-lang.org
- The Rust Programming Language: Validating References with Lifetimes ↗doc.rust-lang.org
- The Rustonomicon: Data Races and Race Conditions ↗doc.rust-lang.org
Now answer it yourself.
Reading a strong answer is easy. Producing one under pressure is the skill the interview tests. Gapmap grades your answer against the same bar an interviewer would.
beta
The interviewer part is in the works.
The diagnostic, personal maps, and AI mock interviews are being finished right now. The notes stay free either way. Leave an email and you'll get the first-cohort invite, plus a month of Pro when it opens.