Which Rust function signatures make lifetime elision fail with E0106, and how should you judge the fix rustc suggests?
answer
- no input, or too many
- positions hidden inside one type
- the help line names the candidates
- tie the output to its real source
basics
~20 sE0106 appears when an elided output lifetime matches no rule: no input lifetime, several input lifetimes with no &self receiver, or two positions inside one parameter. rustc's suggested lifetime compiles but can over-constrain; name only the input the output really borrows from.
solid answer
~40 srustc reports `error[E0106]: missing lifetime specifier` when an elided output lifetime matches no elision rule. The shapes are: **no input lifetime** (`fn default_name() -> &str`), **two or more input lifetimes with no `&self` receiver** (`fn pick(a: &str, b: &str) -> &str`), and **several positions inside one parameter**, as in `&TextBuffer<'_>` or `&mut Vec<&str>`. The same error fires on a bare reference in a struct field, enum variant or type alias, where nothing is ever elided. The help line names the candidates rustc could not choose between. Its suggestion often puts one `'a` on every position, which compiles but can over-constrain callers: for `fn word_at(buf: &TextBuffer<'_>, i: usize) -> &str` the precise fix is `fn word_at<'a>(buf: &TextBuffer<'a>, i: usize) -> &'a str`.
code
plaintext · 11 lineserror[E0106]: missing lifetime specifier
--> src/main.rs:2:47
|
2 | fn word_at(buf: &TextBuffer<'_>, i: usize) -> &str {
| --------------- ^ expected named lifetime parameter
|
= help: this function's return type contains a borrowed value, but the signature does not say which one of `buf`'s 2 lifetimes it is borrowed from
help: consider introducing a named lifetime parameter
|
2 - fn word_at(buf: &TextBuffer<'_>, i: usize) -> &str {
2 + fn word_at<'a>(buf: &'a TextBuffer<'a>, i: usize) -> &'a str {go deeper
Recognise error[E0106]: missing lifetime specifier and its two homes: a signature whose output lifetime cannot be elided, and a struct, enum or alias holding a bare reference.
List the failing shapes, including hidden positions in TextBuffer<'_> or Vec<&str>, and read the help line to see which inputs rustc could not choose between.
Judge rustc's suggestion: name only the lifetime of the input the output truly borrows from, so callers are not forced to keep a wrapper or a vector borrowed.
Expect reviews to check that a lifetime added to silence E0106 states a real borrow relationship, because an over-tight public signature becomes a constraint every caller inherits.
## What E0106 means `error[E0106]: missing lifetime specifier` means a type needs a lifetime that was neither written nor supplied by an elision rule. It has two homes: - **Function signatures**, where an output lifetime is elided but no rule assigns it one. - **Type definitions**: struct fields, enum variants and type aliases never elide, so `struct Line { text: &str }` is always `E0106`, and so is `struct Line { text: &'_ str }`. Only outputs can fail. Elided inputs always succeed, because rule 1 turns each one into a fresh lifetime parameter. The error belongs to the signature itself and is raised before the body is borrow-checked, so `fn pick(a: &str, b: &str) -> &str { todo!() }` fails just the same as a full implementation. ## The failing signature shapes | Shape | Example | What rustc's help says | |---|---|---| | No input lifetime | `fn default_name() -> &str` | there is no value for it to be borrowed from | | Two inputs, no receiver | `fn pick(a: &str, b: &str) -> &str` | does not say whether it is borrowed from `a` or `b` | | Two positions in one parameter | `fn word_at(buf: &TextBuffer<'_>, i: usize) -> &str` | does not say which one of `buf`'s 2 lifetimes it is borrowed from | | Nested reference | `fn last_word(words: &mut Vec<&str>) -> &str` | which one of `words`'s 2 lifetimes | | One named lifetime used twice | `fn both<'a>(a: &'a str, b: &'a str) -> &str` | the lifetime cannot be derived from the arguments | The middle rows surprise people: a single parameter can carry two lifetime positions. `&TextBuffer<'_>` has the outer `&` and the `'_` inside the type, and rule 2 needs exactly one. Writing the output as `&'_ str` does not help either: `'_` in an output follows the same rules as an omitted lifetime. ## Reading the help line rustc's diagnostic for `E0106` is precise once you know its parts: - The dashes under the parameters mark each **candidate input lifetime**. - The caret under the return type marks the **elided output** it could not fill. - The `= help:` line says why: no input, or which inputs it could not choose between. - The `help: consider introducing a named lifetime parameter` block is a **mechanical suggestion**: usually one new `'a` on every candidate and on the output. For a function with no input, it instead suggests `&'static str`, noting that this is uncommon unless you return a borrowed value from a `const` or a `static`, and offers returning an owned `String`. ## When the suggestion over-constrains For `word_at`, rustc suggests `fn word_at<'a>(buf: &'a TextBuffer<'a>, i: usize) -> &'a str`. It compiles, but it ties the borrow of the `TextBuffer` value and the borrowed source text into one lifetime, so the word cannot outlive the `TextBuffer` value, even though it points into the source string. A caller that builds the buffer in an inner block and keeps the word afterwards gets ``error[E0597]: `buf` does not live long enough``. Naming only the inner lifetime, `fn word_at<'a>(buf: &TextBuffer<'a>, i: usize) -> &'a str`, states what the body really does. The nested case is worse. For `last_word` rustc suggests `&'a mut Vec<&'a str>`, which keeps the vector mutably borrowed for as long as its elements' lifetime; a later `v.push("c")` fails with `E0499` even after the returned word is no longer used. The reason is that `&mut T` is invariant in `T`, covered with variance and subtyping. The precise fix is `fn last_word<'w>(words: &mut Vec<&'w str>) -> &'w str`. ## Choosing the fix 1. Decide which input the returned reference **actually points into**, by reading the body. 2. Name a lifetime on **that position only** and on the output; leave every other input elided. 3. If there is no input to borrow from, return `&'static str` for literals and statics, or an owned `String` for text built at run time. 4. If the output can come from several inputs, one shared lifetime is correct, and choosing it is a matter of explicit annotations rather than elision. Whichever fix you choose, check it against a caller that keeps the result longer than usual. A signature that compiles can still be tighter than the data, and the cost only shows up where a caller holds a word longer than the wrapper or vector it came through.
- Why does `fn word_at(buf: &TextBuffer<'_>, i: usize) -> &str` fail when it has only one reference parameter?`&TextBuffer<'_>` holds two lifetime positions: the outer `&` and the `'_` inside the type. Rule 2 needs exactly one input lifetime, so rustc says the signature does not say which one of `buf`'s 2 lifetimes the result is borrowed from. Name the inner one: `fn word_at<'a>(buf: &TextBuffer<'a>, i: usize) -> &'a str`.
- What does rustc suggest for `fn default_name() -> &str`, and which suggestion fits?It suggests `&'static str`, noting that this is uncommon unless the function returns a borrowed value from a `const` or a `static`, and alternatively returning an owned `String`. For a string literal `&'static str` is exactly right; for text computed at run time return `String`, because there is no input it could borrow from.
- Why is the suggested `&'a mut Vec<&'a str>` a poor fix for `fn last_word(words: &mut Vec<&str>) -> &str`?It ties the mutable borrow of the vector to the lifetime of the strings inside it. Because `&mut T` is invariant in `T`, that lifetime cannot shrink, so the vector stays mutably borrowed long after the call; a later `v.push("c")` fails with `E0499` even when the returned word is no longer used. `fn last_word<'w>(words: &mut Vec<&'w str>) -> &'w str` avoids it.
- E0106 means the function body returns a reference to a local variable.
- Applying rustc's suggested lifetime is always the most flexible fix.
- A struct field of type &str can rely on elision like a function parameter.
- fn default_name() -> &str should default to 'static on its own.
- Only parameters written with & add input lifetimes; TextBuffer<'_> adds none.