Skip to Content
E0106: Missing Lifetime Specifier

error[E0106]: missing lifetime specifier

A function returns a reference, or a struct holds one, and Rust can’t tell what it borrows from. You need to name the lifetime, or own the data instead.

Quick fix: Add a lifetime parameter: fn longest<'a>(a: &'a str, b: &'a str) -> &'a str, or return or store an owned String.

Code that triggers E0106

src/main.rs
fn longest(a: &str, b: &str) -> &str { if a.len() >= b.len() { a } else { b } } fn main() { println!("{}", longest("borrow", "checker")); }

What the compiler says

error[E0106]: missing lifetime specifier --> src/main.rs:1:33 | 1 | fn longest(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 longest<'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`.

Why Rust raises E0106

When a function returns a reference, Rust needs to know which input it borrows from, so it can check that callers don’t use the result after that input is gone. With a single reference parameter it works this out on its own (lifetime elision). With two, a and b, it can’t, and the help text says exactly that.

A lifetime annotation doesn’t make anything live longer. 'a is a label that says: the returned reference is valid for as long as both inputs are.

Structs hit the same error when a field is a reference:

src/main.rs
struct Excerpt { text: &str, } fn main() { let excerpt = Excerpt { text: "Call me Ishmael." }; println!("{}", excerpt.text); }
error[E0106]: missing lifetime specifier --> src/main.rs:2:11 | 2 | text: &str, | ^ expected named lifetime parameter | help: consider introducing a named lifetime parameter | 1 ~ struct Excerpt<'a> { 2 ~ text: &'a str, | error: aborting due to 1 previous error For more information about this error, try `rustc --explain E0106`.

How to fix E0106

1. Name the lifetime

Add a lifetime parameter 'a and tie both inputs and the output to it. This is the compiler’s own suggestion and the standard fix for functions like longest.

src/main.rs
fn longest<'a>(a: &'a str, b: &'a str) -> &'a str { if a.len() >= b.len() { a } else { b } } fn main() { println!("{}", longest("borrow", "checker")); }

2. Give the struct a lifetime parameter

For a struct that borrows, add a lifetime parameter. The compiler then makes sure an Excerpt never outlives the string it points into.

src/main.rs
struct Excerpt<'a> { text: &'a str, } fn main() { let novel = String::from("Call me Ishmael. Some years ago..."); let first = novel.split('.').next().unwrap_or(""); let excerpt = Excerpt { text: first }; println!("{}", excerpt.text); }

3. Or store owned data

Often the simplest fix for a struct is to own the data: a String field instead of &str. You pay for one allocation and skip the lifetime bookkeeping entirely.

src/main.rs
struct Excerpt { text: String, } fn main() { let excerpt = Excerpt { text: String::from("Call me Ishmael.") }; println!("{}", excerpt.text); }

Learn the concept behind it

The References and Borrowing lesson explains the rules behind E0106 with diagrams and more examples. For the official reference, run rustc --explain E0106 or read the E0106 entry in the Rust error index .

Hit a different error? Paste it into the Rust error lookup to find its fix.

Last updated on