I'm new to Rust. I would like to know the lifetime of &1 value in the below code snippet. Even though &1 is declared in the inner scope, it lives beyond that scope. Why &1 is not getting dropped once the inner scope ends? Looks like it has to do with primitive types, not sure what exactly is going on here.
fn main() {
let e;
let y = String::new();
{
e = Extreme::new(&String::new(), &y);
}
println!("{:?}", e);
}
Note that it's the String created in the inner block that is problematic, not the reference. You can create a reference in the inner block, store it in e, and have it be valid beyond the block -- provided it is referencing something that is also valid beyond the block. The lifetime of a reference is not limited to the block in which it is created.