fn longest<'a>(x: &'a str, y: &'a str) -> &'a str {
if x.len() > y.len() {
x
} else {
y
}
}
fn main() {
let str1 = String::from("long str");
let long_str;
{
let str2 = String::from("str");
long_str = longest(str1.as_str(), str2.as_str());
}
println!("long_str is {}", long_str); //long_str is long str
}
error[E0597]: `str2` does not live long enough
--> src\main.rs:14:43
|
14 | long_str = longest(str1.as_str(), str2.as_str());
| ^^^^^^^^^^^^^ borrowed value does not live long enough
15 | }
| - `str2` dropped here while still borrowed
16 | println!("long_str is {}", long_str); //long_str is long str
| -------- borrow later used here
For more information about this error, try `rustc --explain E0597`.
warning: `bollow` (bin "bollow") generated 1 warning
error: could not compile `bollow` due to previous error; 1 warning emitted