In example shown in Listing 1.13 we don't need lifetime for y
It should be
fn longest_common_prefix<'a>(x: &'a str, y: &str) -> &'a str`
This is because function does not return reference to y. Code works for current example, but it will not work if we replace main() with this:
fn main() {
let string1 = String::from("abc");;
let result;
{
let string2 = String::from("abdef");
result = longest_common_prefix(&string1, &string2);
}
println!("The longest common prefix is: {}", result);
}
because string2 don't live as long as result. In current example it only works because both strings are static.
Current example makes lifetimes confusing and example meaningless for lifetimes.
In example shown in Listing 1.13 we don't need lifetime for y
It should be
This is because function does not return reference to y. Code works for current example, but it will not work if we replace main() with this:
because string2 don't live as long as result. In current example it only works because both strings are static.
Current example makes lifetimes confusing and example meaningless for lifetimes.