Skip to content
Theory · Lifetimes · lesson 4 of 6

Lifetime elision

In the borrowing theme there was a function head(s: &str, n: usize) -> &str, and it had no 'a at all. Why does it compile, while longest without an annotation does not?

In a library, most tickets are routine. If a reader brings one ticket, the librarian puts the same date on the new one and asks nothing. The librarian asks only when there are two tickets and it is unclear whose date to take.

Elision rules

The elision rules are three rules by which the compiler writes lifetimes into a signature by itself. It applies them to the function header, never reads the body and guesses nothing:

  1. Each reference parameter gets a lifetime of its own.
  2. If there is exactly one reference parameter, the result gets its lifetime too.
  3. If one of the parameters is &self or &mut self, the result gets the lifetime of self.

If after the three rules a reference in the result still has no lifetime, the build stops with error E0106.

One reference in

Rust
fn head(s: &str, n: usize) -> &str {
    &s[..n]
}

fn head_full<'a>(s: &'a str, n: usize) -> &'a str {
    &s[..n]
}

fn main() {
    let s = String::from("rust book");
    println!("{} {}", head(&s, 4), head_full(&s, 4)); // rust rust
}

Let's go through what the compiler did with head:

  1. The number n is not a reference and takes no part in the rules, so there is one reference parameter, s.
  2. Rule 1 gives s a lifetime.
  3. Rule 2 passes it on to the result.

The result is exactly the signature of head_full, and to the compiler the two functions are the same.

A method with &self

The method fn name(&self, prefix: &str) -> &str takes two references, but one of them is &self. Rule 3 ties the result to self: a method usually returns something from its own struct, so there is nothing to guess.

Two references, no self

For fn longest(x: &str, y: &str) -> &str, rule 1 gives x and y different lifetimes. Rule 2 does not apply: there are two references. Neither does rule 3: there is no self among the parameters. The result is left with no lifetime, and the compiler stops: error[E0106]: missing lifetime specifier.

This is an error, not a warning. The compiler does not look into the body to work out which input comes back, and the program does not build. You write the link explicitly, with 'a.

Interviewers like to ask this: "Why does fn first(s: &str) -> &str compile without annotations?" The answer: there is one reference in, and rule 2 gives its lifetime to the result.

Common mistake

A method with &self returns not a field of its struct but another parameter:

Rust
struct Shelf {
    name: String,
}

impl Shelf {
    fn pick(&self, title: &str) -> &str {
        title // ✗
    }
}

fn main() {
    let s = Shelf { name: String::from("A") };
    println!("{} {}", s.name, s.pick("Dune"));
}
TEXT
error: lifetime may not live long enough

The program does not compile. Rule 3 tied the result to self, and the header promises a reference valid as long as self. But title may turn out shorter, so the compiler rejects the method. The fix is to name the link explicitly: fn pick<'a>(&self, title: &'a str) -> &'a str.

Key points

  • The elision rules look only at the signature: with one reference in, or with &self, you do not write 'a.
  • With &self the result gets the lifetime of self; to return another parameter, you write the link explicitly.
  • Two reference inputs with no self and a reference in the result give error E0106, not a warning.