BorrowCheck › Rust language core

Borrowing, references, NLL

Immutable vs mutable reference rules, non-lexical lifetimes, reborrowing, splitting borrows.

The topic explains borrowing: at any moment a value may have either any number of shared references &T or exactly one mutable reference &mut T. The questions check why &T implements Copy while &mut T does not, and what happens to the owner while the value is mutably borrowed. The subtopic on non-lexical lifetimes (NLL) shows that a borrow lasts until the reference's last use rather than to the end of the block, and covers cases where a destructor or a conditional return of a reference extends it. The subtopics on reborrowing and splitting borrows describe the implicit reborrow at a call site, its absence for a generic parameter, independent &mut references to different struct fields, split_at_mut and iter_mut. A separate part covers dangling references and temporaries, including temporary lifetime extension, as well as two-phase borrows, which are what makes the call v.push(v.len()) compile. The topic builds on ownership and prepares for explicit lifetimes.

36 questions: 16 basic, 16 intermediate, 4 advanced. 6 info pages, 11 sources.

Subtopics

  1. Immutable vs mutable references
  2. Non-lexical lifetimes (NLL)
  3. Reborrowing
  4. Splitting borrows
  5. Dangling references
  6. Two-phase borrows

Previous: Ownership, move, Copy/Clone · Next: Lifetimes, subtyping, variance