In the Rust book, such an example for lifetime annotation (LA) is given as below.
It uses LA for a valid reason, because neither the compiler nor we know whether a
or b
will be returned in the run time.
fn longest<'a>(x: &'a str, y: &'a str) -> &'a str {
if x.len() > y.len() {
x
} else {
y
}
}
However, if this function simply returns y
or even a static &str, the compiler still expects a named LA parameter in the return.
Why can't it read the function content when compiling and make the right decision for us?
fn longest<'a>(x: &'a str, y: &'a str) -> &'a str {
y
// "str slice" <-- or return a static value
}