Rust References Lifetimes
Rust has a mechanism called borrow checker
that makes sure references are not used when they are not valid anymore. The borrow checker uses lifetimes to do its job internally.
Let’s look at a simple example where the borrow checker detects a possibly invalid reference:
1
2
3
4
5
6
7
8
9
10
fn main() {
let r;
{
let i = 1;
r = &i;
}
println!("{}", r);
}
If we compile this, we’ll get the following error: