fn main() {
let strA = "a";
let result;
{
let strB = "abc";
result = longest(strA, strB); // Will return strB
}
println!("The longest string is {}", result); // result now point to strB!!
}
fn longest<'a>(x: &'a str, y: &'a str) -> &'a str {
if x.len() > y.len() {
x
} else {
y
}
}
As I got from the Rust book
'a
will get the concrete lifetime that is equal to the smaller of the lifetimes ofx
andy
So why strB
is now visible outside of its scope?
That's because all string literals have
'static
lifetime. From the rust book: