Sub topic 7
Where a Borrow Ends: Non-Lexical Lifetimes
This chapter assumes you know shared references (&T), mutable references (&mut T), and the rule that a value has either many shared borrows or one mutable borrow at a time. Here you learn how the compiler decides when a borrow stops counting against that rule.
A borrow lasts until its last use
Early versions of Rust ended a borrow at the closing brace of the block where the reference was declared. Modern Rust uses non-lexical lifetimes (NLL). Under NLL, a borrow is live from the point it is created until the last point where the reference is used. "Non-lexical" means the lifetime is not tied to the braces in the source text. It follows the paths the program can take.
fn main() {
let mut s = String::from("a");
let r = &s;
println!("{r}");
let m = &mut s;
m.push('b');
println!("{m}");
}This compiles. The shared borrow r is last used in the first println!. After that line it is dead, so &mut s does not overlap with it. Both references are still in scope, but only live borrows count.
When a later use keeps the borrow alive
Now move a use of the shared reference to after the mutation.
fn main() {
let mut v = vec![1, 2, 3];
let first = &v[0];
v.push(4);
println!("{first}");
}This does not compile. The compiler reports error E0502: it cannot borrow v as mutable because it is also borrowed as immutable. The final println! uses first, so the shared borrow is live across v.push(4). Pushing may reallocate the vector's buffer, which would leave first pointing at freed memory. The rule prevents exactly that.
Fixing the error
You do not need to change the types. Change the order so the last use of the reference comes before the mutation.
fn main() {
let mut v = vec![1, 2, 3];
let first = &v[0];
println!("{first}");
v.push(4);
println!("{:?}", v);
}If you need the value after the push, copy it out instead of holding a reference. For an i32, let first = v[0]; copies the number and creates no borrow.
What NLL does not change
NLL changes where borrows end. It does not change what conflicts. Two live borrows of the same value, where at least one is mutable, are still an error.
NLL also follows control flow. A borrow is live only on the paths where it can still be used later. A reference used in one if branch does not block a mutation in the other branch.
Reading the error with NLL in mind
When you see E0502 or its sibling E0499 (two mutable borrows), look for three places in the message: where the borrow starts, where the conflicting use happens, and where the reference is used later. The compiler marks each one. To fix the error, end the first borrow earlier, or avoid holding the reference at all by copying or cloning the data you need.
Exercise
This code fails with E0502. Name the line that keeps the shared borrow alive, then change the code so it compiles without cloning. let mut s = String::from("hi"); let r = &s; s.push('!'); println!("{r}");
Show answer
The final println! uses r, so the shared borrow is live across s.push('!'). Move the println! above the push, so r's last use comes before the mutable borrow. The fixed code is: let mut s = String::from("hi"); let r = &s; println!("{r}"); s.push('!');