The canonical example I see when dealing with Rust lifetimes is the following func:
pub fn longest<'a>(x: &'a str, y: &'a str) -> &'a str {
if x.len() > y.len() {
x
} else {
y
}
}
Interestingly, this seems to translate to "the lifetime of the result of longest() is the shortest lifetime of it's input parameters". The logic makes sense to me but I don't understand how this is communicated by the syntax.
When I look at the function it looks like there is one lifetime 'a and the function seems to be enforcing that x and y must have the exact same lifetime 'a and so the result must have the same lifetime 'a.
I'm not sure if this syntax is essentially syntax sugar for what I'm seeing. If I was allowed to be verbose, then I think the syntax would look something like (it's not correct syntax, I know):
pub fn longest<'a, 'b>(x: &'a str, y: &'b str) -> min('a, 'b) str {
if x.len() > y.len() {
x
} else {
y
}
}
Am I understanding this correctly or am I mis-interpreting the language?