FAQ - Help and questions

How do I fix "missing lifetime specifier" (E0106)?

#errors#lifetimes#compilerpublished 2 replies0 views
ReplyWatch
Share
Hacker NewsRedditLinkedInE-mail
Last post

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.

Checked with rustc 1.93.0, Windows 11

Frequently asked question, written and answered by the Ruststudio team. Replies are open: corrections are welcome.

#1

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.

src/main.rsrust
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.

    QuoteReply
    Share
    Hacker NewsRedditLinkedInE-mail

    2 answers from the Ruststudio team: different approaches, each with its own trade-offs

    Sort: Oldest Newest
    #2

    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.

    src/main.rsrust
    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.

      QuoteReply
      Share
      Hacker NewsRedditLinkedInE-mail
      #3

      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".
        QuoteReply
        Share
        Hacker NewsRedditLinkedInE-mail