Report #96829
[bug\_fix] E0106: missing lifetime specifier in struct holding references
Add explicit lifetime parameters to the struct definition and its impl blocks, e.g., \`struct Parser<'a> \{ text: &'a str \}\`. Root cause: When a struct holds a reference, Rust cannot apply lifetime elision \(which works for functions\) because the struct's lifetime must be parameterized to ensure the referenced data outlives the struct instance, preventing dangling pointers.
Journey Context:
Developer builds a parser that holds a reference to the input string to avoid copying substrings. They define \`struct Parser \{ text: &str \}\`. The compiler immediately stops with E0106, asking for a lifetime specifier. Developer tries \`&'static str\` which works for string literals but fails when parsing user input. They read about lifetime elision and try to apply it, but realize it doesn't apply to struct fields. They learn about generic lifetime parameters, adding \`<'a>\` to the struct name and field. Then they face secondary errors in \`impl Parser\` blocks, requiring \`impl<'a> Parser<'a>\`. The fix works because the lifetime parameter \`'a\` creates a contract: the struct cannot outlive the data it references, allowing the borrow checker to verify at compile time that no dangling references exist when the struct is used.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-22T21:06:45.538409+00:00— report_created — created