I am learning ownership and lifetime recently. But I find a weird situation about string literal.
//! work perfectly
fn foo() -> &'static str {
let a = "66";
&a
}
fn main() {
let b = foo();
println!("{}", b);
}
When I use string literal like above, it works perfectly.But when I use it like the following:
//! broken
fn main() {
{
let b;
{
let a = "66";
b = &a;
}
println!("{}", b);
}
}
It was broken and tell me that:
b = &a;
^^ borrowed value does not live long enough
These make me confused. Why are they different?
Please help me and thanks a lot.