# How do I fix "missing lifetime specifier" (E0106)?
Source: https://ruststudio.org/community/t/138/how-do-i-fix-missing-lifetime-specifier-e0106
Author: Ruststudio Team (staff) | Published: 2026-12-05 | Updated: 2026-12-05 | Checked with: rustc 1.93.0, Windows 11
Category: Help and questions | Kind: FAQ | Tags: errors, lifetimes, compiler | Views: 0

## Short answer
A function returns a reference, but the compiler cannot tell which parameter the reference borrows from. Add a lifetime parameter such as 'a to the inputs and the output that are connected. If the result should not borrow from the inputs at all, return an owned value like String instead.

## Answer
## Answer
Every reference has a lifetime, the stretch of code in which it is valid. Usually the compiler works it out. When a function takes two or more references and returns one, it cannot know which input the result is tied to, so you say so. A lifetime name like `'a` is only a label that connects the places that must live equally long.

```rust title=src/main.rs
fn longest(a: &str, b: &str) -> &str {
    if a.len() >= b.len() { a } else { b }
}

fn main() {
    println!("{}", longest("tree", "forest"));
}
```

```text
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`.
```

The answers below are the two ways out.

## Answers (2)

### Answer 1 by Ruststudio Team (staff), 2026-12-05, 0 votes
## Approach 1: name the lifetime
Say that both inputs and the result are tied together. The result then lives no longer than the shorter of the two inputs.

```rust title=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("tree", "forest"));
}
```

```text
forest
```

Best when: the result really is a piece of one of the inputs and you want to avoid copying.

### Answer 2 by Ruststudio Team (staff), 2026-12-05, 0 votes
## Approach 2: return an owned value
If the function builds something new, or copying is cheap enough, return a `String` (or `Vec`, or a number) instead of a reference. Owned values have no lifetime to explain.

```rust
fn longest(a: &str, b: &str) -> String {
    if a.len() >= b.len() { a.to_string() } else { b.to_string() }
}
```

Best when: the caller wants to keep the result after the inputs are gone, or the lifetime annotations make the signature hard to use.

## Why it works
When there is exactly one input reference, the compiler assumes the output borrows from it, and no annotation is needed. With a `&self` method the output is assumed to borrow from `self`. Annotations are only for the cases that remain ambiguous.

## Common mistakes
- Using `'static` to make the error go away. It means "valid for the whole program" and is wrong for data that is borrowed from a local variable.
- Returning a reference to a local variable. The local is freed when the function ends, and no lifetime can fix that; see the thread on "does not live long enough".
