Just got done reading the rust book chapter on lifetimes.
Everything made mostly made sense, in particular one of the main examples being that the following will not compile
fn longest<'a>(x: &'a str, y: &'a str) -> &'a str {
if x.len() > y.len() {
x
} else {
y
}
}
fn main() {
let string1 = String::from("long string is long");
let result;
{
let string2 = String::from("xyz");
result = longest(string1.as_str(), string2.as_str());
}
println!("The longest string is {}", result);
}
This is because "We’ve told Rust that the lifetime of the reference returned by the longest function is the same as the smaller of the lifetimes of the references passed in. Therefore, the borrow checker disallows the code in Listing 10-24 as possibly having an invalid reference." And in another part of the chapter "the lifetime of the reference returned by the longest function is the same as the smaller of the lifetimes of the references passed in. "
So naturally I try to come up with my own simplified example as follows
fn foo<'a>(x: &'a str) -> &'a str {
x
}
fn main() {
let y;
{
let x = "hello";
y = foo(x);
}
println!("y: {}", y);
}
And to my surprise this compiles and runs just fine. How? Shouldn't the reference y have its lifetime end when x goes out of scope?