A Rust tokenizer `fn next_token<'a>(src: &'a str, delims: &'a str) -> &'a str` causes E0597 at callers that drop the delimiter `String` early; why, and what fixes it?
answer
- callers checked against the signature
- one 'a ties every input
- token only slices src
- give delims its own lifetime
basics
~20 sThe shared 'a tells rustc the token may borrow from delims as well as src, so callers must keep the delimiter String alive as long as the token. Giving delims its own or an elided lifetime ties the output to src alone.
solid answer
~40 srustc checks calls against the signature, not the body. A single `'a` on `src`, `delims` and the output says the token may borrow from either input, so at each call `'a` must fit inside both borrows; a caller that drops `delims` before using the token gets `E0597: delims does not live long enough`, even though the body only slices `src`. The fix is in the signature: `fn next_token<'a>(src: &'a str, delims: &str) -> &'a str` (or an explicit `'b` on `delims`). Callers may then drop `delims` right after the call, and if the body ever tried to return `delims`, rustc would reject the body with "lifetime may not live long enough" instead of blaming callers.
code
rust · 14 linesfn next_token<'a>(src: &'a str, delims: &'a str) -> &'a str {
let end = src.find(|c| delims.contains(c)).unwrap_or(src.len());
&src[..end]
}
fn main() {
let source = String::from("key=value;next");
let token;
{
let delims = String::from("=;");
token = next_token(&source, &delims);
}
println!("{token}");
}go deeper
Recall that a lifetime on an input tells rustc the output may borrow from it, so only inputs the output really comes from should share its lifetime.
Explain that rustc checks each call against the signature alone, which is why an over-tight annotation surfaces as E0597 at a caller instead of inside the function.
Diagnose a call-site E0597 against an argument the result never points into as a signature defect, fix it by splitting lifetimes, and reject the 'b: 'a suggestion that restores the coupling.
Make honest lifetime relations part of API review: loosening later is safe for callers, but tightening breaks them, so the first published signature should tie only what it must.
## The symptom A tokenizer returns the next token as a slice of its source, stopping at any delimiter character: ```rust fn next_token<'a>(src: &'a str, delims: &'a str) -> &'a str { let end = src.find(|c| delims.contains(c)).unwrap_or(src.len()); &src[..end] } ``` The function compiles and its tests pass. Then a caller builds the delimiter set in a short-lived block and keeps the token: ```rust let source = String::from("key=value;next"); let token; { let delims = String::from("=;"); token = next_token(&source, &delims); } println!("{token}"); ``` rustc 1.99.1 rejects the caller: ```plaintext error[E0597]: `delims` does not live long enough ``` The caller looks guilty, but the token never points into `delims`. The real defect is in the signature, and it survived testing for a simple reason: unit tests tend to pass string literals for both arguments, and literals are valid for the whole program, so an over-tight relation never bites until a caller builds its delimiters at run time. ## The cause: a signature that over-promises rustc checks every call against the **signature**, not the body. One `'a` on `src`, `delims` and the output tells rustc "the result may borrow from either input", so at each call `'a` must fit inside *both* borrows. The body only ever slices `src`, but rustc is not allowed to look; the contract says the token depends on `delims`, so a caller that drops `delims` first must be rejected. This is a **returned reference tied to the wrong input**: - the annotation is not unsound: it only forbids more than necessary; - every caller pays for it, by keeping an unrelated value alive as long as the result; - the error appears far from the cause, at a call site, often in another module. ## The fix: tie the output only to what it borrows Give `delims` its own lifetime, either explicitly or by leaving it elided: ```rust fn next_token<'a, 'b>(src: &'a str, delims: &'b str) -> &'a str { /* same body */ } // or, equivalently: fn next_token<'a>(src: &'a str, delims: &str) -> &'a str { /* same body */ } ``` Now the output depends only on `src`. The caller above compiles and prints `key`; `delims` may be dropped as soon as the call returns. ## The compiler now guards the body Splitting the lifetimes moves the check to where it belongs. If a later edit made the body return `delims`, say as a fallback for an empty source, rustc rejects the **body** instead of the callers: ```plaintext error: lifetime may not live long enough = help: consider adding the following bound: `'b: 'a` ``` This diagnostic carries no E-code. Its suggestion deserves care: adding `'b: 'a` would make the body compile but restore the original coupling, because callers would again have to keep `delims` alive as long as the token. The honest choices are to keep returning slices of `src` only, or to return an owned `String` when the delimiters can be part of the answer. The single shared `'a` hid that decision; the split forces it. ## Reviewing signatures for this defect | Signature | Output may borrow from | Caller may drop `delims` early? | |---|---|---| | `next_token<'a>(src: &'a str, delims: &'a str) -> &'a str` | `src` or `delims` | no, E0597 | | `next_token<'a, 'b: 'a>(src: &'a str, delims: &'b str) -> &'a str` | `src` or `delims` | no, E0597 | | `next_token<'a>(src: &'a str, delims: &str) -> &'a str` | `src` only | yes | | `next_token<'a>(src: &str, delims: &'a str) -> &'a str` | `delims` only | body fails with E0621 | A checklist for code review: 1. For each reference in the output, find the inputs the body can actually return data from. 2. Put the output's lifetime on exactly those inputs; give every other reference its own or an elided lifetime. 3. Treat a call-site E0597 against an argument the result never points into as a signature bug, not a caller bug. Loosening an over-tight signature this way keeps every direct call that compiled before compiling, since callers are only asked for less. Tightening one in the other direction can break existing callers, which is why the relation is worth getting right when a function is first published.
- After the split, rustc suggests adding 'b: 'a when the body returns delims. Should you accept it?Usually not. The bound makes the body compile but requires `delims` to outlive the token again, so callers regain the original E0597. Either keep the body returning slices of `src` only, or return an owned `String` when the delimiters can be part of the result.
- Is changing the shared 'a into separate lifetimes a breaking change for callers?No for direct calls: the new signature asks callers for less, so every call that compiled before still compiles. The reverse change, tying an extra input to the output, can break existing callers that drop that input early.
- Why does rustc not infer from the body that the token only depends on src?Function signatures are the boundary of the borrow check. Inferring relations from bodies would make a caller's validity depend on implementation details, so a body edit could break callers far away. The signature is the promise callers rely on.
- The caller is wrong because E0597 names its argument.
- rustc sees from the body that only src is sliced.
- Adding 'b: 'a is the right fix for the caller's error.
- Separate lifetimes on src and delims make the function unsound.
- Loosening the signature breaks callers that compiled before.