Fix a signature that ties the returned &str to the wrong input
first_word returns the part of s before the first sep. The author gave each parameter its own lifetime but tied the result to sep, and the function no longer compiles: error: lifetime may not live long enough. Say what the result really borrows from, and correct the signature.
Requirements:
mainstays as it is and printshello:wordis used aftersepis dropped.- Change only the signature of
first_word; its body stays the same. - Do not name a lifetime that elision can supply.
fn first_word<'a, 'b>(s: &'a str, sep: &'b str) -> &'b str {
s.split(sep).next().unwrap_or(s)
}
fn main() {
let line = String::from("hello world");
let word;
{
let sep = String::from(" ");
word = first_word(&line, &sep);
}
println!("{word}");
}Find and fix the bug.
The result is a slice of s (split yields parts of s), so it borrows from s, not sep: write fn first_word<'a>(s: &'a str, sep: &str) -> &'a str. Adding 'a: 'b or one shared 'a compiles the body but still ties word to sep (E0597).
- Applying the compiler's
'a: 'bhint, which fixes the body but keepswordtied tosep - Putting one
'aon every reference, which makessepoutlive every use of the result - Annotating the separator even though the result never borrows from it
- Why can an
impl Iterator<Item = &'a str>result in edition 2024 stay tied to an unrelated&str? - Why is the compiler's suggested bound
'a: 'bnot the fix this caller needs?
split(sep) cuts s, and every part is a slice of s itself; the separator only says where to cut. So the result borrows from s, and the output lifetime must be 'a. The separator needs no name: rule 1 gives it its own lifetime, and the output no longer uses it.
fn first_word<'a>(s: &'a str, sep: &str) -> &'a str {
s.split(sep).next().unwrap_or(s)
}
fn main() {
let line = String::from("hello world");
let word;
{
let sep = String::from(" ");
word = first_word(&line, &sep);
}
println!("{word}"); // hello
}Two "fixes" get the body through but break the call. The compiler's hint 'a: 'b allows returning 'a where 'b is promised, but the result is still valid only for 'b, that is, while sep lives, and main gets error[E0597]: `sep` does not live long enough. One shared 'a on both inputs gives the same error: 'a cannot be longer than sep's life. The signature has to tell the truth about what the result borrows from — then the caller only needs line to stay alive.
In edition 2024 the same trap hides in return-position impl Trait: such a type captures every lifetime in the signature, elided ones included.
fn evens<'a>(v: &'a [i32], label: &str) -> impl Iterator<Item = &'a i32> + use<'a> {
println!("{label}");
v.iter().filter(|x| *x % 2 == 0)
}
fn main() {
let v = vec![1, 2, 3, 4];
let it;
{
let label = String::from("evens");
it = evens(&v, &label);
}
for x in it {
println!("{x}"); // 2, then 4
}
}Without + use<'a> the iterator would count as depending on label, and the call would fail with E0597, even though Item = &'a i32 says nothing about label.