Report #96471
[bug\_fix] missing lifetime specifier, expected named lifetime parameter
Add an explicit lifetime parameter to the struct or function, e.g., \`struct Parser<'a> \{ content: &'a str \}\` and propagate \`'a\` to all \`impl\` blocks. Root cause: When a struct holds a reference, the compiler cannot infer how long that reference lives relative to the struct; explicit lifetimes create a contract ensuring the data outlives the struct.
Journey Context:
Developer, comfortable with simple functions, decides to create a zero-copy parser that holds a reference to a source string to avoid allocations. They write \`struct Parser \{ source: &str, pos: usize \}\`. The compiler immediately throws "missing lifetime specifier". Developer is confused because \`&str\` worked in function arguments without annotations. They try \`&'static str\`, which compiles but later fails when they try to parse user input that isn't static. They search and find blog posts titled "Fighting the Borrow Checker," entering a rabbit hole of complex lifetime elision rules. They randomly add \`'a\` lifetimes: \`struct Parser<'a> \{ source: &'a str \}\`. The struct definition compiles, but their \`impl Parser\` block now errors with "missing lifetime specifier". They learn they must declare the lifetime on the impl: \`impl<'a> Parser<'a>\`. Finally, they must ensure methods that return internal references also use \`'a\` or new lifetimes like \`fn peek\(&self\) -> &'a str\`. The fix works because the explicit \`'a\` annotates the relationship between the struct instance and the borrowed data, guaranteeing the reference remains valid for the entire scope where the struct is used, preventing use-after-free. The root cause is that structs can be passed around, stored in other structs, or returned from functions; without explicit lifetime parameters, the compiler cannot track that the borrowed data outlives these complex movements.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-22T20:30:42.149230+00:00— report_created — created