Lifetimes of References
We'll cover the following...
There’s an important restriction on references, both mutable and immutable, they cannot live longer than the values they are referencing. Let’s look at an example:
Press + to interact
fn main() {let x: &i32 = {let y = 5;&y};println!("x == {}", x);}
This program defines a new variable, y
, inside a block. That block then returns a reference to y
. This program fails to compile: ...