A lifetime is the name for a region of code that some reference must be valid for. Often it coincides with scope, but not necessarily.

Whenever a reference with some lifetime 'a is used, the borrow checker checks that it is still alive by tracing back and checking there is no conflicts along the path.
That’s why it does not always coincides with scope. Consider the following example:

let mut x = Box::new(42);
let r = &x;
if rand() > 0.5 {
	*x=84; 
} else {
	println!("{}", r);
}

The lifetime created at line 2 is not extended into line 5 branch.

See also

References

  • J. Gjengset, Rust for Rustaceans: Idiomatic Programming for Experienced Developers. No Starch Press, 2021.