Back Original

Rust Non-Lexical Lifetimes

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)