Core Rust Questions · 14 min · free with an account

Why Borrowing Works

Lesson preview · about 30%

The borrowing answer is about aliasing versus mutation.

Core idea

Aliasing XOR mutation, checked over the region a borrow is used.

Start with the idea

Say it as a rule about coexistence: you may have many readers or one writer, never both, and the compiler tracks the region where each borrow is live. That single constraint is what makes data races impossible for safe code without any runtime cost.

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.

Keep learning, for free

The rest of this lesson is waiting for you.

Create a free account to read the complete explanation, see the infographic, explore the code, and try the exercises. All 742 lessons and algorithm steps are included.

No payment details. Your account opens the complete Academy.

← All lessons in Rust Interview Prep