So supposed I have this function:
fn longest(x: &str) -> &str {
x
}
This will run fine if I call it.
However, if I do this:
fn longest(x: &str, y: &str) -> &str {
x
}
I get the error
error[E0106]: missing lifetime specifier
--> src/main.rs:1:33
|
1 | fn longest(x: &str, y: &str) -> &str {
| ---- ---- ^ expected named lifetime parameter
|
= help: this function's return type contains a borrowed value, but the signature does not say whether it is borrowed from `x` or `y`
help: consider introducing a named lifetime parameter
|
1 | fn longest<'a>(x: &'a str, y: &'a str) -> &'a str {
| ++++ ++ ++ ++
Why is that? Shouldn't rust know that the return is x
so the lifetime will follow x
?