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
'awill get the concrete lifetime that is equal to the smaller of the lifetimes ofxandy
So why strB is now visible outside of its scope?
That's because all string literals have
'staticlifetime. From the rust book: