The Rust Programming Language says:
fn longest<'a>(x: &'a str, y: &'a str) -> &'a str { if x.len() > y.len() { x } else { y } }
The function signature now tells Rust that for some lifetime
'a
, the function takes two parameters, both of which are string slices that live at least as long as lifetime'a
. The function signature also tells Rust that the string slice returned from the function will live at least as long as lifetime'a
. In practice, it means that the lifetime of the reference returned by thelongest
function is the same as the smaller of the lifetimes of the references passed in. These relationships are what we want Rust to use when analyzing this code.
I don't get why it says:
In practice, it means that the lifetime of the reference returned by the
longest
function is the same as the smaller of the lifetimes of the references passed in.
Note the word "smaller". For both parameters and returned values, we specified 'a
which is the same. Why does the book say "smaller"? If that was the case, we would have different specifiers(a'
, b'
).