Email me

Rust errors / Everything else

Rust compiler error · checked with rustc 1.99.0

Rust error E0772: #### Note: this error code is no longer emitted by the compiler

What it means#### Note: this error code is no longer emitted by the compiler.

Retired: rustc no longer emits this error code. You may still see it in old answers, books and CI logs from older toolchains; the explanation below is kept for reference.

E0772Everything else
Retiredin rustc 1.99.0
8related errors linked below

The compiler team's explanation

Note: this error code is no longer emitted by the compiler.

A trait object has some specific lifetime '1, but it was used in a way that requires it to have a 'static lifetime.

Example of erroneous code:

trait BooleanLike {}
trait Person {}

impl BooleanLike for bool {}

impl dyn Person {
    fn is_cool(&self) -> bool {
        // hey you, you're pretty cool
        true
    }
}

fn get_is_cool<'p>(person: &'p dyn Person) -> impl BooleanLike {
    // error: `person` has an anonymous lifetime `'p` but calling
    //        `print_cool_fn` introduces an implicit `'static` lifetime
    //        requirement
    person.is_cool()
}

The trait object person in the function get_is_cool, while already being behind a reference with lifetime 'p, also has it's own implicit lifetime, '2.

Lifetime '2 represents the data the trait object might hold inside, for example:

trait MyTrait {}

struct MyStruct<'a>(&'a i32);

impl<'a> MyTrait for MyStruct<'a> {}

With this scenario, if a trait object of dyn MyTrait + '2 was made from MyStruct<'a>, 'a must live as long, if not longer than '2. This allows the trait object's internal data to be accessed safely from any trait methods. This rule also goes for any lifetime any struct made into a trait object may have.

In the implementation for dyn Person, the '2 lifetime representing the internal data was omitted, meaning that the compiler inferred the lifetime 'static. As a result, the implementation's is_cool is inferred by the compiler to look like this:

fn is_cool<'a>(self: &'a (dyn Person + 'static)) -> bool {unimplemented!()}

While the get_is_cool function is inferred to look like this:

fn get_is_cool<'p, R: BooleanLike>(person: &'p (dyn Person + 'p)) -> R {
    unimplemented!()
}

Which brings us to the core of the problem; the assignment of type &'_ (dyn Person + '_) to type &'_ (dyn Person + 'static) is impossible.

Fixing it is as simple as being generic over lifetime '2, as to prevent the compiler from inferring it as 'static:

impl<'d> dyn Person + 'd {/* ... */}

// This works too, and is more elegant:
//impl dyn Person + '_ {/* ... */}

See the [Rust Reference on Trait Object Lifetime Bounds][trait-objects] for more information on trait object lifetimes.

From rustc --explain E0772 (1.99.0), part of the Rust project, MIT or Apache-2.0. Official page.

Related errors

All 518 Rust error codes · Rust crate data · Glossary

Up next

Often hit nextE0061: this function takes N arguments but M were suppliedA related error from the same family.Read it next →

Did this answer your question?

Get the receipts by email.

What my AI agents tried this week, with the real numbers: what worked, what flopped, what made money. One email when there is something worth reading.

No spam, never sold. Every email has a one-click unsubscribe.

Popular in the shop

All 124 tools →

Browse: Bundles and kitsHome, family and schoolMoney and businessAI toolsSports and fitnessMaking and hobbiesFood and drinkIndie dev and launchTrading and quant

Free: Landing page teardown iPhone app cost calculator LLM API cost calculator Paid online: $49 Launch Check App builds Articles FAQ