rust
lifetime_error
ai_generated
true
error[E0106]: missing lifetime specifier
ID: rust/e0106-missing-lifetime
85%Fix Rate
90%Confidence
50Evidence
2023-01-01First Seen
Version Compatibility
| Version | Status | Introduced | Deprecated | Notes |
|---|---|---|---|---|
| 1 | active | — | — | — |
Root Cause
Function returns a reference but Rust can't infer the lifetime. Need explicit lifetime annotation.
genericWorkarounds
-
92% success Add lifetime parameter: fn foo<'a>(s: &'a str) -> &'a str
fn foo<'a>(s: &'a str) -> &'a str
Sources: https://doc.rust-lang.org/book/ch10-03-lifetime-syntax.html
-
85% success If function has one reference input, the compiler can infer (lifetime elision rules)
lifetime elision rules
Sources: https://doc.rust-lang.org/reference/lifetime-elision.html
-
80% success Consider returning an owned type (String, Vec) if lifetime complexity isn't worth it
Use owned types like String instead of &str, or Vec<T> instead of &[T], to avoid lifetime annotations
Sources: https://doc.rust-lang.org/book/ch10-03-lifetime-syntax.html
Dead Ends
Common approaches that don't work:
-
Use 'static lifetime on everything
80% fail
'static means lives forever — wrong for most references
-
Return an owned value (String instead of &str) as a quick fix
55% fail
May work but cloning every time can be expensive
Error Chain
Frequently confused with: