2026-02-13 Rust Non-Lexical Lifetimes

Sorry for lack of content, I’m still alive, life has just been very hard. I will try to get back to writing more ❤️.

I was doing a quiz on rust lifetimes last night, and got the following question wrong. Will this code compile?

let mut v = vec![1, 2, 3];
let a = &v[0];
println!("{a}");
v.push(4);
Click here to see the answer :)

I thought it would not compile, because a takes read ownership, and push needs read + write access. But I was wrong!

It does compile! It’s due to a Rust feature called Non-Lexical Lifetimes (added in 2018). So along with the other rules, the borrow checker also asks: “Is there any path where the borrow is used after this point?” If the answer is no, the borrow ends there!

  let mut v = vec![1, 2, 3];
  let a = &v[0];      // immutable borrow starts
  println!("{a}");    // last use of `a` - borrow ENDS here with NLL
  v.push(4);          // RW mutable borrow is fine, no conflict

I hope this helps others learning Rust (its a beautiful language)