Answer#
Every reference has a lifetime, the stretch of code in which it is valid. Usually the compiler works it out. When a function takes two or more references and returns one, it cannot know which input the result is tied to, so you say so. A lifetime name like 'a is only a label that connects the places that must live equally long.
fn longest(a: &str, b: &str) -> &str {
if a.len() >= b.len() { a } else { b }
}
fn main() {
println!("{}", longest("tree", "forest"));
}error[E0106]: missing lifetime specifier
--> src/main.rs:1:33
|
1 | fn longest(a: &str, b: &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 `a` or `b`
help: consider introducing a named lifetime parameter
|
1 | fn longest<'a>(a: &'a str, b: &'a str) -> &'a str {
| ++++ ++ ++ ++
error: aborting due to 1 previous error
For more information about this error, try `rustc --explain E0106`.The answers below are the two ways out.