fn main()
{
let long;
let str1 = "12323".to_string();
{
let str2 = "asdhuisdihuds".to_string();
long = longest(&str1, &str2);
println!("{}", long);
}
}
fn longest(x: &str, y: &str) -> &str // Error over here
{
if x.len() > y.len()
{
x
}
else
{
y
}
}
I understand I have an error here and I can fix this by doing this:
fn longest<'a>(x: &'a str, y: &'a str) -> &'a str
{
if x.len() > y.len()
{
x
}
else
{
y
}
}
But I don't quite get why do I have to specify its lifetime for &str
?