Reference validity and lifetime relationships
Quick answer
A reference must stay valid for every use. The borrow checker verifies that the referenced value lives long enough and that access follows Rust's aliasing rules.
Think about the last use
A lifetime describes a region of valid use, not a clock. A shared borrow can end after its last use before the enclosing block ends. An owned String dropped at the end of an inner block cannot be borrowed afterward.
fn main() { let name = String::from("Mina"); let view = &name; println!("{view}"); drop(name);}Expected stdout (not executed):
MinaThe reference is no longer used when the owner is dropped.
fn main() { let view; { let name = String::from("Mina"); view = &name; } println!("{view}");}Expected: compilation fails; exact diagnostic not verified.
The inner String does not live long enough. Move the owner outside the block instead.
Common mistake
Declaring the reference outside a block does not extend the owner's lifetime.
Try it yourself
Draw owner and reference lifelines for both programs and mark the last use.
Static examples for Rust 1.85.0, edition 2024. Run them locally in a disposable project; ByteMind execution support and these outputs have not been verified.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Reference validity and lifetime relationships — practice quiz
Choose one answer per question, then review the explanation.
Does a lifetime annotation keep an owned String alive longer?
When may a shared reference stop preventing mutation?
Why is returning a reference to an owned local String invalid?