Answer#
The borrow checker is a compile-time check, not a runtime one: when it rejects code, the program would have had a memory bug (a dangling reference, or two parts of the program changing the same data at once) if the compiler had let it through.
It enforces three rules:
- Every value has exactly one owner. Assigning a value to another variable, or passing it to a function by value, moves it; the old name can no longer be used.
- At any moment you can have either any number of shared references (
&T) or one mutable reference (&mut T) to a value, never both at the same time. - A reference can never outlive the value it points to.
The compiler error tells you which rule was broken and points at the lines involved. The four messages below are the ones people meet first. They were produced by rustc 1.93.0 on Windows 11, shortened where long.
| Code | Message (start) | Rule broken |
|---|---|---|
| E0382 | borrow of moved value: name | 1: the value was moved, then used again |
| E0499 | cannot borrow v as mutable more than once at a time | 2: two &mut at once |
| E0502 | cannot borrow scores as mutable because it is also borrowed as immutable | 2: & and &mut at once |
| E0505 | cannot move out of s because it is borrowed | 3: the value was moved or dropped while a reference to it is still used later |
This is the most common one:
fn main() {
let mut scores = vec![10, 20, 30];
let first = &scores[0]; // a shared borrow starts here...
scores.push(40); // ...a mutable borrow is needed here (push may move the data)
println!("{first}"); // ...and the shared borrow is still used here
}error[E0502]: cannot borrow `scores` as mutable because it is also borrowed as immutable
--> src/main.rs:4:5
|
3 | let first = &scores[0];
| ------ immutable borrow occurs here
4 | scores.push(40);
| ^^^^^^^^^^^^^^^ mutable borrow occurs here
5 | println!("{first}");
| ----- immutable borrow later used hereHere push may need to allocate a bigger buffer and move all elements, which would leave first pointing at freed memory. The compiler cannot know that push will not do that, so it refuses. The answers below show three different ways to fix it; which one fits depends on what the code is really doing.
Each error code has a long explanation: run rustc --explain E0502.
Common mistakes#
- Fighting the checker with
clone()everywhere. A clone is a valid fix when you need an independent copy, but cloning only to silence an error can hide a design problem. - Reaching for
unsafeor raw pointers to get around an error. The rules exist because the code would be unsound; restructure instead. - Reading only the first line of the error. The labels under the code lines say where the borrow starts and where it is used later, and that is where the fix goes.
Related questions#
Does the borrow checker slow my program down?#
No. It runs only while compiling and produces no code, so there is no runtime cost.
What does the lifetime error "borrowed value does not live long enough" mean?#
That is rule 3: a reference was kept after the value it points to was dropped, for example by returning a reference to a local variable. Return the value itself (or a copy) instead, or make the owner live longer than the reference.
Sources#
- The Rust Book, "Understanding Ownership": https://doc.rust-lang.org/book/ch04-00-understanding-ownership.html
- The Rust Book, "References and Borrowing": https://doc.rust-lang.org/book/ch04-02-references-and-borrowing.html
- Error code index: https://doc.rust-lang.org/error_codes/E0502.html