I want to understand more about lifetime
I got this piece of code:
fn longest<'a>(x: &'a str, y: &'a str) -> &'a str {
if x.len() > y.len() {
x
} else {
y
}
}
i just want to know why the following code compile:
fn main() {
let x = "hello world";
let result;
{
let y = "foo";
result = longest(x, y);
}
println!("result is '{}'", result);
}
and this one is not:
fn main() {
let x = String::from("Hello world");
let result;
{
let y = String::from("foo");
result = longest(&x, &y);
}
println!("result is '{}'", result);
}