Consider the following example from the Book:
fn main() {
let string1 = String::from("abcd");
let string2 = "xyz";
let result = longest(string1.as_str(), string2);
println!("The longest string is {}", result);
}
fn longest<'a>(x: &'a str, y: &'a str) -> &'a str {
if x.len() > y.len() {
x
} else {
y
}
}
It is said that (emphasis mine)
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 constraints are what we want Rust to enforce.
Shouldn't the bolded sentence be The function signature also tells Rust that the string slice returned from the function will live at most as long as lifetime 'a
.? That way, we are assured that as long as both x
and y
are alive, then the return value would also be valid, because the latter references the former.
To paraphrase, if x
, y
and the return value all live at least as long as lifetime 'a
, then the compiler can simply let 'a
be an empty scope (which any item can outlive) to satisfy the restriction, rendering the annotation useless. This doesn't make sense, right?
x
andy
. – Discrepantx
andy
are valid. It may become invalid as soon as one ofx
ory
is no longer valid, but it may also live longer (in particular if the other one is still valid). – Exaltx
andy
are still valid. – Exaltx
andy
can live at most as long as'a
? If all ofx
,y
, and the return value can outlive'a
, then the annotation doesn't seem extremely helpful to me, because it cannot function as a "lifetime barrier". – Discrepantx
andy
must (not "can") live at least as long as'a
. Yes,x
,y
and the return value can outlive'a
. Forget about'a
, the point of the annotation is to link the lifetime of the return value to the lifetimes ofx
andy
. What the annotation means is literally: "you can use the return value as long as bothx
andy
remain valid. As soon as eitherx
ory
becomes invalid, then the return value may be invalid and so can no longer be used safely".'a
is just a placeholder that represents the lifetime when all three values are guaranteed to be valid. – Exalta < b
anda < c
tells us nothing about the relationship betweenb
andc
, so it's weird that lifetime annotations can establish linkage from the fact that both the argument and the return value can outlive some arbitrary lifetime. – Discrepantfor all 'a, 'a≤'x and 'a≤'y implies 'a≤'r
(with'x
,'y
and'r
the lifetimes ofx
,y
, and the return value respectively). For that relation to hold for all'a
, then you must necessarily have'x≤'r
or'y≤'r
– Exaltlongest
function doesn’t need to know exactly how longx
andy
will live, only that some scope can be substituted for'a
that will satisfy this signature.. Do you think the "some scope" here should be "all scopes"? – Discrepant