E0106 Missing Lifetime Fix
A reference in the code carries no lifetime the compiler can work out - because a struct or enum stores a reference and declares no lifetime at all, or because a function returns a reference and its signature does not say which input that reference is borrowed from. This guide separates four shapes that produce E0106 and matches each one to the lifetime declaration the diagnostic itself points at.
Written November 23, 2026 · Tested with Rust 1.99.0 · 20 min read
- The rule underneath: elision fills in a lifetime only where the answer is forced
- How to read the diagnostic
- The first failure: a struct with a reference field and no lifetime
- The second failure: a function returning a reference, with two reference parameters
Written November 23, 2026
Tested with Rust 1.99.0
What this guide is
Error E0106 is a missing lifetime. The error codes index states the cause in one line: this error indicates that a lifetime is missing from a type, and if the error is inside a function signature, the problem may be with failing to adhere to the lifetime elision rules. Every reference has a lifetime - the scope for which that reference is valid - and a signature or a type definition has to leave the compiler a way to work that lifetime out. Where it cannot, the code does not compile until a lifetime is named.
The four forms in this guide each reproduce E0106 with a small, complete program, and each fails at a different place. A struct whose field is a reference, with no lifetime declared on the struct. A function that returns a reference and takes two reference parameters, so the return value could come from either. An enum with a reference in one variant, with no lifetime declared on the enum. And a function whose return value can come from a field of a struct parameter or from a second reference parameter, again with no lifetime tying them together. In each family the failing program is small, the full compiler output is shown, and the fix names one lifetime and puts it on the references it relates: on the struct or enum and its reference, or on the function, its parameters, and its return type.
Two neighboring codes are not E0106 and are not in the families. Annotating only one of two parameters a return value can come from is E0621, an explicit lifetime required in the type of the other parameter, and a value dropped while it is still borrowed is E0597: in both, the failure is not a missing lifetime name, and a different rule is what fails. E0106 is only about the missing name - a lifetime the compiler was never given and cannot infer.
Build every program in this guide with rustc --edition 2021. In the compiler transcripts below, the file names are the bare names shown on the command line.
The rule underneath: elision fills in a lifetime only where the answer is forced
Lifetime elision is the restricted inference the compiler applies to function signatures. The Rust Reference section on lifetime elision gives the rules: each elided lifetime in the parameters becomes a distinct lifetime parameter; if there is exactly one lifetime used in the parameters, elided or not, that lifetime is assigned to all elided output lifetimes; and in a method, if the receiver has type &Self or &mut Self, the lifetime of that reference to Self is assigned to all elided output lifetime parameters. The Reference adds the boundary in one sentence: it is an error to elide lifetime parameters that cannot be inferred.
The Rust Book chapter on lifetimes states the same three rules in the same order and draws the same line: if the compiler works through the rules and a lifetime is still undetermined, the result is an error, and the programmer supplies the annotation. The Book also states what an annotation does and does not do: lifetime annotations do not change how long any of the references live; they describe the relationships of the lifetimes of multiple references to each other, so the borrow checker can reject values that do not adhere to those relationships.
Struct and enum definitions stand outside these rules. Elision applies to function signatures; a type that stores a reference has no parameters and no return value for the rules to work from, so the lifetime has to be declared on the type itself, as a generic lifetime parameter, and written on the stored reference. The error index works exactly this pair: a struct field written as &bool is its erroneous example, and the same field written as &'a bool inside a struct that declares <'a> is its correct form; its enum example is the same shape, a variant holding &bool corrected to a variant holding &'a bool in an enum that declares <'a>.
So every E0106 repair has the same shape: name the lifetime the compiler could not infer, and write it in the places that have to agree - on the type that stores the reference, or on the function and on each reference its return value can be borrowed from. No value, type, or control flow changes; only the lifetime relationship is stated.
How to read the diagnostic
The header is the same in every family: "missing lifetime specifier". What separates the families is the caret span and the help. The caret marks the exact reference that has no lifetime: a field type in a struct, a variant payload in an enum, or the return type of a function, where the label reads "expected named lifetime parameter".
Read the help next. In a struct or an enum, the help is "consider introducing a named lifetime parameter", and it rewrites the definition with the lifetime added - the parameter on the type name and the annotation on the reference. In a function, the help first explains the gap in a note: "this function's return type contains a borrowed value, but the signature does not say whether it is borrowed from" one input or another, naming the candidates; then "consider introducing a named lifetime parameter" rewrites the whole signature with one lifetime on the parameters and the return type. Where a struct parameter is one of the candidates, the note counts the lifetimes inside that parameter as candidates too, as the fourth family shows.
The first failure: a struct with a reference field and no lifetime
The field text is a reference, and the struct declares no lifetime parameter. There is no signature for elision to work on - a stored reference has to carry its lifetime on the type - so the field type is where the error lands:
struct Holder {
text: &str,
}
fn main() {
let h = Holder { text: "hello" };
println!("{}", h.text);
}
$ rustc --edition 2021 fail_struct.rs -o fail_struct
error[E0106]: missing lifetime specifier
--> fail_struct.rs:2:11
|
2 | text: &str,
| ^ expected named lifetime parameter
|
help: consider introducing a named lifetime parameter
|
1 ~ struct Holder<'a> {
2 ~ text: &'a str,
|
error: aborting due to 1 previous error
For more information about this error, try `rustc --explain E0106`.
The caret sits on the &str of the field, and the help rewrites both lines: the lifetime parameter <'a> on the struct name, and the annotation on the field. Follow it exactly:
struct Holder<'a> {
text: &'a str,
}
fn main() {
let h = Holder { text: "hello" };
println!("{}", h.text);
}
$ rustc --edition 2021 fix_struct.rs -o fix_struct
$ ./fix_struct
hello
The output is the value the failing program was trying to print: hello, the text the field holds. The struct now states that a Holder is valid only while the text it borrows is valid, which is the relationship the field type alone could not express. Every use of the struct carries that parameter from here on - a function that takes a Holder names it as Holder<'a>, as the fourth family does.
The second failure: a function returning a reference, with two reference parameters
pick can return either parameter: the if returns a and the else returns b. Elision gives each parameter its own distinct lifetime under the first rule, and then stops - the second rule needs exactly one input lifetime, and the third needs a &self receiver, and this signature has neither:
fn pick(a: &str, b: &str) -> &str {
if a.len() > b.len() { a } else { b }
}
fn main() {
println!("{}", pick("longer", "short"));
}
$ rustc --edition 2021 fail_pick.rs -o fail_pick
error[E0106]: missing lifetime specifier
--> fail_pick.rs:1:30
|
1 | fn pick(a: &str, b: &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 `a` or `b`
help: consider introducing a named lifetime parameter
|
1 | fn pick<'a>(a: &'a str, b: &'a str) -> &'a str {
| ++++ ++ ++ ++
error: aborting due to 1 previous error
For more information about this error, try `rustc --explain E0106`.
The diagnostic underlines both parameters with dashes and puts the caret on the return type, and the note states the gap in the compiler's own words: the signature does not say whether the return value is borrowed from a or b. The help rewrites the signature with one lifetime in four places: the parameter declaration after the function name, both parameters, and the return type. Follow it exactly:
fn pick<'a>(a: &'a str, b: &'a str) -> &'a str {
if a.len() > b.len() { a } else { b }
}
fn main() {
println!("{}", pick("longer", "short"));
}
$ rustc --edition 2021 fix_pick.rs -o fix_pick
$ ./fix_pick
longer
The output is the value the failing program was trying to print: longer, the first argument, which is longer than short. The one lifetime states the relationship the Book describes for this shape: the returned reference is valid as long as both parameters are valid, because either one can be the value returned. The body does not change - it was already returning one of the two references; only the signature now says so.
The third failure: an enum with a reference variant and no lifetime
An enum is in the same position as the struct in the first family. The variant Text holds a reference, and the enum declares no lifetime parameter, so the payload type has no lifetime to carry:
enum Choice {
Text(&str),
Empty,
}
fn main() {
let choices = [Choice::Text("hello"), Choice::Empty];
for c in choices {
match c {
Choice::Text(t) => println!("{}", t),
Choice::Empty => println!("empty"),
}
}
}
$ rustc --edition 2021 fail_enum.rs -o fail_enum
error[E0106]: missing lifetime specifier
--> fail_enum.rs:2:10
|
2 | Text(&str),
| ^ expected named lifetime parameter
|
help: consider introducing a named lifetime parameter
|
1 ~ enum Choice<'a> {
2 ~ Text(&'a str),
|
error: aborting due to 1 previous error
For more information about this error, try `rustc --explain E0106`.
The caret sits on the &str inside the variant, and the help rewrites the definition the same way it did for the struct: <'a> on the enum name and &'a str as the payload. Follow it exactly:
enum Choice<'a> {
Text(&'a str),
Empty,
}
fn main() {
let choices = [Choice::Text("hello"), Choice::Empty];
for c in choices {
match c {
Choice::Text(t) => println!("{}", t),
Choice::Empty => println!("empty"),
}
}
}
$ rustc --edition 2021 fix_enum.rs -o fix_enum
$ ./fix_enum
hello
empty
The output is the pair of lines the failing program was trying to print: hello for the Text value, then empty for the Empty value. The variant that holds no reference needs no annotation; the lifetime belongs to the enum as a whole, because any Choice value might be the variant that borrows. Declaring the parameter on the enum and the annotation on the one reference is the whole repair.
The fourth failure: a return value drawn from a struct parameter and a second reference
Here the struct from the first family is already fixed - Holder declares its lifetime - and the failure moves back into a function signature. pick_text returns the struct's field when it is not empty and the fallback parameter when it is, and the signature names no lifetime at all: not on the Holder argument, not on fallback, and not on the return type:
struct Holder<'a> {
text: &'a str,
}
fn pick_text(h: &Holder, fallback: &str) -> &str {
if h.text.is_empty() { fallback } else { h.text }
}
fn main() {
let h = Holder { text: "hello" };
println!("{}", pick_text(&h, "fallback"));
}
$ rustc --edition 2021 fail_from_struct.rs -o fail_from_struct
error[E0106]: missing lifetime specifier
--> fail_from_struct.rs:5:45
|
5 | fn pick_text(h: &Holder, fallback: &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 one of `h`'s 2 lifetimes or `fallback`
help: consider introducing a named lifetime parameter
|
5 | fn pick_text<'a>(h: &'a Holder<'a>, fallback: &'a str) -> &'a str {
| ++++ ++ ++++ ++ ++
error: aborting due to 1 previous error
For more information about this error, try `rustc --explain E0106`.
The note counts the candidates, and there are three, not two: the signature does not say whether the return value is borrowed from one of h's 2 lifetimes or fallback. The struct parameter carries two lifetimes of its own - the reference to the struct, and the lifetime declared on the struct - and either could be the one the field's reference follows. The help answers by tying all of them to one name. The fix below states the same relationship and leaves only the reference to the struct itself elided: that outer reference gets its own distinct lifetime under the first elision rule, and no rule has to assign it to the output, because the return type's lifetime is written out in full:
struct Holder<'a> {
text: &'a str,
}
fn pick_text<'a>(h: &Holder<'a>, fallback: &'a str) -> &'a str {
if h.text.is_empty() { fallback } else { h.text }
}
fn main() {
let h = Holder { text: "hello" };
println!("{}", pick_text(&h, "fallback"));
}
$ rustc --edition 2021 fix_from_struct.rs -o fix_from_struct
$ ./fix_from_struct
hello
The output is the value the failing program was trying to print: hello, the field of the Holder built in main, because that field is not empty and the fallback branch is not taken. One lifetime now runs through the signature: the Holder argument carries it, fallback carries it, and the return type carries it, so the returned reference is valid as long as both sources are valid - the same relationship as the second family, with one source reached through a struct field instead of a direct parameter.
The fixes in reach order
- The caret is on a field type or a variant payload: declare a lifetime parameter on the struct or enum, and write that lifetime on the stored reference.
- The caret is on a return type and the note names two parameters: declare one lifetime on the function, and write it on both parameters and on the return type.
- The note counts lifetimes inside a struct parameter as candidates: write the function's lifetime on the struct argument as the struct's own lifetime argument, as well as on the other parameters and the return type.
- The return value comes from only one parameter: that parameter alone carries the lifetime, and the return type carries it - and if a second parameter can also be returned, leaving it unannotated is E0621, as the traps section shows.
- No reference needs to be stored or returned at all: own the data instead - a
Stringfield in place of a&strfield removes the lifetime from the type entirely, as the traps section shows.
Traps
- One reference parameter and a reference return is not E0106 - it compiles with nothing written. The second elision rule assigns the one input lifetime to the output, so this function needs no annotation at all:
fn first(s: &str) -> &str {
s
}
fn main() {
println!("{}", first("hello"));
}
$ rustc --edition 2021 probe_first.rs -o probe_first
$ ./probe_first
hello
hello, the argument passed through. Adding a lifetime here is allowed but unnecessary; the contrast is the diagnostic point: E0106 starts where elision stops, and with exactly one input lifetime elision does not stop.
- A method returning a reference is not E0106 either, even with the reference stored in the struct. The third elision rule assigns the lifetime of &self to the elided output lifetime, so the method signature needs no annotation - the struct still declares its own lifetime, because the stored field is outside elision, as the first family showed:struct Holder<'a> {
text: &'a str,
}
impl<'a> Holder<'a> {
fn text(&self) -> &str {
self.text
}
}
fn main() {
let h = Holder { text: "hello" };
println!("{}", h.text());
}
$ rustc --edition 2021 probe_method.rs -o probe_method
$ ./probe_method
hello
The output is hello, the field value returned through the method. The rule covers the method's return type only; it does not remove the lifetime from the struct definition, and it does not extend to a free function with the same body.
- Annotating only one of the parameters the return value can come from is a different error, not a partial fix. With 'a on a and on the return type but not on b, the missing-lifetime error is gone - and the body now fails against the signature, because the else branch returns a reference the signature does not tie to 'a:
fn pick<'a>(a: &'a str, b: &str) -> &'a str {
if a.len() > b.len() { a } else { b }
}
fn main() {
println!("{}", pick("longer", "short"));
}
$ rustc --edition 2021 probe_partial.rs -o probe_partial
error[E0621]: explicit lifetime required in the type of `b`
--> probe_partial.rs:2:39
|
2 | if a.len() > b.len() { a } else { b }
| ^ lifetime `'a` required
|
help: add explicit lifetime `'a` to the type of `b`
|
1 | fn pick<'a>(a: &'a str, b: &'a str) -> &'a str {
| ++
error: aborting due to 1 previous error
For more information about this error, try `rustc --explain E0621`.
The header names the new code and the new defect: E0621, "explicit lifetime required in the type of b", and the help adds the missing annotation to b. Follow it, and the signature is the fixed signature from the second family:
fn pick<'a>(a: &'a str, b: &'a str) -> &'a str {
if a.len() > b.len() { a } else { b }
}
fn main() {
println!("{}", pick("longer", "short"));
}
$ rustc --edition 2021 fix_partial.rs -o fix_partial
$ ./fix_partial
longer
The output is longer. The step from E0106 to E0621 is the signature gaining a lifetime that the body does not fully match: E0106 is a lifetime missing from a type, and E0621 is a mismatch between the lifetimes in the signature and the data flow in the body, which is how the error codes index defines it.
- Storing owned data removes the question instead of answering it. A struct that holds a String stores no reference, so there is no lifetime to declare and no parameter on the type:
struct Holder {
text: String,
}
fn main() {
let h = Holder { text: String::from("hello") };
println!("{}", h.text);
}
$ rustc --edition 2021 probe_owned.rs -o probe_owned
$ ./probe_owned
hello
The output is hello. This is a design change, not an annotation: the struct now owns its text and is valid for as long as it itself exists, at the cost of holding a copy or the original allocation rather than borrowing. Where the borrowed form is wanted - the caller keeps the data and the struct only views it - the lifetime form from the first family is the repair; where nothing else needs the data, owning it is the simpler type.
Decision summary
- The caret is on a struct field or an enum payload: the type stores a reference and declares no lifetime - declare the parameter on the type and annotate the reference.
- The caret is on a function return type and the note names two candidate parameters: elision cannot choose - declare one lifetime and write it on both parameters and the return type.
- The note counts a struct parameter's own lifetimes among the candidates: the return value can come through that struct - write the function's lifetime as the struct's lifetime argument as well.
- There is exactly one reference parameter, or the function is a method on
&self: elision already assigns the output lifetime - no annotation is needed, and an E0106 report means the shape is a different one. - The report is E0621 instead: a lifetime in the signature does not cover a value the body returns - annotate the parameter the diagnostic names, or return only values the stated lifetimes cover.
- The report is E0107 instead: a wrong number of generic arguments was provided for an item - the error codes index gives that as its definition, and its examples include a lifetime argument supplied where none is expected; count the arguments against the item's declaration rather than adding a lifetime to a type.
- The report is E0597 instead: a value was dropped while it was still borrowed - the error codes index gives that as its definition; no lifetime name is missing, and the repair is to keep the borrowed value alive until the borrow is last used, not to add an annotation.
Checklist
- The caret location identified: a stored reference in a type, or a return type in a signature - the repair site follows from which one it is.
- A struct or enum that stores a reference checked to declare a lifetime parameter, with that lifetime written on the stored reference.
- A function returning a reference checked against the elision rules: exactly one input lifetime, or a
&selfreceiver - and where neither holds, one lifetime declared and written on every parameter the return value can come from, and on the return type. - A struct parameter the return value can come through checked to carry the function's lifetime as its lifetime argument.
- A partial annotation checked for the E0621 it produces: every branch of the body returning only references the signature's lifetimes cover.
- The choice between borrowing and owning made deliberately: a stored
Stringneeds no lifetime, and a stored&stralways needs one. - The fixed program run, and its output checked against what the failing program was trying to compute.
Related guides on this site
- Fix Rust E0277 "the trait bound ... is not satisfied" — Missing Trait Impl, Missing Ord, the ? Conversion, Sized, and Clone
- E0107 Wrong Generics Fix
- E0061 Wrong Argument Count Fix
- E0432 Unresolved Import Fix
Sources
- Rust, Error codes index: E0106 - the definition of the error: a lifetime is missing from a type, and inside a function signature the problem may be failing to adhere to the lifetime elision rules; its examples correct a struct field and an enum variant from a bare reference to a reference annotated with a lifetime the type declares, and state that an elided output lifetime requires exactly one input lifetime, or multiple input lifetimes with a
&selfor&mut selfreceiver. - The Rust Reference, Lifetime elision - the elision rules themselves: each elided lifetime in the parameters becomes a distinct lifetime parameter; exactly one lifetime used in the parameters is assigned to all elided output lifetimes; in a method, the lifetime of the
&Selfor&mut Selfreceiver is assigned to all elided output lifetime parameters; and it is an error to elide lifetime parameters that cannot be inferred. - The Rust Programming Language, Validating References with Lifetimes - every reference has a lifetime, the scope for which that reference is valid; the two-parameter function returning a reference as the worked E0106 example, fixed by one lifetime on both parameters and the return value; annotations describe the relationships of the lifetimes of multiple references to each other without changing how long any reference lives; and the lifetime elision rules, with an undetermined lifetime after the rules as an error.
- Rust, Error codes index: E0621 - the neighboring mismatch: a mismatch between the lifetimes appearing in the function signature and the data flow found in the function body, fixed by updating the signature so the parameter the body returns from carries the lifetime the return type states.
- Rust, Error codes index: E0107 - the neighboring count error: an incorrect number of generic arguments was provided, with the exact same number required when using or declaring an item with generic arguments; its examples include a lifetime argument supplied to a function that expects none.
- Rust, Error codes index: E0597 - the neighboring lifetime-length error: a value was dropped while it was still borrowed, fixed by keeping the borrowed value in scope until after the borrow is last used.
Keep Exploring
The full Rust hub \u2014 ownership, structs and enums, async, and measured comparisons.
Every guide on the site in one list, grouped by topic.
Every browser-only checker and verifier on the site.
The C++, Python, Sysadmin, Rust, Go, and TypeScript hubs.