Rust "error[E0106]: missing lifetime specifier" in CI
A function return type or a struct field holds a reference, but the compiler cannot infer how long it lives. Lifetime elision does not apply here, so you must name the lifetime explicitly.
What this error means
cargo build fails with error[E0106]: missing lifetime specifier, pointing at a & in a return type or struct definition and noting expected named lifetime parameter. The build stops.
error[E0106]: missing lifetime specifier
--> src/lib.rs:1:24
|
1 | fn first(s: &str, t: &str) -> &str {
| ---- ---- ^ expected named lifetime parameter
= help: this function's return type contains a borrowed value, but the
signature does not say whether it is borrowed from `s` or `t`Common causes
Ambiguous borrow source in a return type
With more than one reference input, elision cannot decide which the returned reference borrows from, so you must annotate the lifetime.
Reference field in a struct
A struct that stores a &T must declare a lifetime parameter; without it the compiler cannot reason about how long the borrow is valid.
How to fix it
Add an explicit lifetime
Tie the output (or field) to the input it borrows from.
fn first<'a>(s: &'a str, t: &str) -> &'a str { s }
struct Holder<'a> { name: &'a str }Or return an owned value
If the borrow relationship is awkward, return an owned type and avoid lifetimes.
fn first(s: &str, _t: &str) -> String { s.to_owned() }How to prevent it
- Annotate lifetimes when a return type borrows from one of several inputs.
- Add a lifetime parameter to any struct holding references.
- Return owned values where the lifetime plumbing is not worth it.