
Understand it
Shared reads and exclusive mutation of the same memory cannot overlap incompatibly. Rust tracks the uses that keep each borrow live, rather than banning both reference kinds from one lexical scope. Interior mutability provides additional APIs whose safety rules must still be respected.
Borrowing separates temporary access from ownership. With shared references, several readers may coexist. A mutable reference requires exclusive access to the same value for the region where it is live. Rust tracks uses of borrows, so the relevant region can end before the closing brace when the reference is no longer used. That is why a read can be followed by a mutation in one scope if the read borrow has truly ended. The rule concerns overlapping access, not a ban on ever having both forms in one function.
Show the rule on a vector. Hold a shared reference to its first element, then attempt to push another element while that reference is still needed. A push may reallocate the vector, invalidating an element reference; the compiler rejects that overlap. Read or copy the needed value first, let the borrow end, and then push. This is a concrete safety story the interviewer can follow.
Be careful with broad claims. Safe Rust prevents unsynchronized data races, but borrowing alone does not prove the program is logically correct, deadlock-free, or free of runtime panics. Interior mutability types may enforce their access rules at runtime. If asked how to fix a borrow error, first draw owner, readers, writer, and last uses. Then narrow a scope, change an API to borrow less, or move data only when that matches the desired ownership. A clone is one option, not the default answer.
A borrowed reference also cannot outlive its owner. Explain this lifetime relationship when the example crosses a function boundary.
Interview follow-up: Print first before the push. Why does that version compile without adding clone?
Read the code
let mut v = vec![1, 2];
let a = &v[0];
let b = &v[1]; // two readers: fine
println!("{a} {b}");
v.push(3); // writer, after the readers endMany readers, or one writer — never at the same time.
Practice starter
fn main() {
let mut items = vec!["crab", "rust"];
let first = &items[0];
items.push("compiler");
println!("{first}");
}Try it
Repair the program so it prints crab while keeping the items.push( call and the println!. Then state the rule it broke.
What success looks like: It prints crab, and you can name aliasing, mutation, and where each borrow ends.
Check your understanding
Which overlapping uses make this program fail to compile?
Reveal the answer
- A shared reference and a push can never appear in one lexical scope.
- The shared borrow of items[0] is still used after push needs a mutable borrow.
- Creating a reference to a Vec requires cloning its allocation first.
println! uses first after the push, so the shared borrow overlaps the mutable one. Without that later use, this program would compile under non-lexical lifetimes.
Go deeper
Common misconception: Creating a reference to a Vec requires cloning its allocation first.
&items[0] borrows the element in place, with no clone. The error comes from that borrow overlapping push, not from creating the reference.
- Identify the contract: The borrowing answer is about aliasing versus mutation.
- Trace the concrete example: Many readers, or one writer — never at the same time.
- Test the changed constraint: Print
firstbefore thepush. Why does that version compile without adding clone?
Transfer challenge: Print first before the push. Why does that version compile without adding clone?
Connections
- Explain Ownership: Moves, Copy, Drop, and scope
- Lifetime Reasoning: Relationships, not object duration
Learn this lesson with AI
Paste this into Claude, Codex, Cursor or another agent connected to Crabrix Academy. It contains only this lesson’s public details.
I'm learning Rust with Crabrix Academy. Teach me this lesson with the Crabrix Academy tools. Lesson: Why Borrowing Works (courseId "interview", lessonId "q-borrowing") Link: https://crabrix.com/learn/interview/q-borrowing/ Goal: Aliasing XOR mutation, checked over the region a borrow is used. Practice: open the starter in the Crabrix Playground (https://crabrix.com/playground/?lesson=interview/q-borrowing) and I will write the code there. Explain it step by step for my level, let me write the code myself, give hints before any solution, and do not mark anything as passed: only Crabrix checks do that.