Email me

Rust errors / Ownership and borrowing

Rust compiler error · checked with rustc 1.99.0

Rust error E0803: cannot infer an appropriate lifetime for lifetime parameter '_ in generic type due to conflicting requirements

What it meansA trait implementation returns a reference without an explicit lifetime linking it to self. It commonly arises in generic trait implementations requiring explicit lifetime bounds.

What rustc prints

error[E0803]: cannot infer an appropriate lifetime for lifetime parameter '_ in generic type due to conflicting requirements
  --> main.rs:11:5
   |
11 |     fn get_ref(&self) -> &f64 { // Error: Lifetime mismatch
   |     ^^^^^^^^^^^^^^^^^^^^^^^^^
   |
note: first, the lifetime cannot outlive the lifetime `'a` as defined here...
  --> main.rs:10:6
   |
10 | impl<'a> DataAccess<&f64> for Container<'a> {
   |      ^^
note: ...so that the reference type `&Container<'a>` does not outlive the data it points at
  --> main.rs:11:5
   |
11 |     fn get_ref(&self) -> &f64 { // Error: Lifetime mismatch
   |     ^^^^^^^^^^^^^^^^^^^^^^^^^
note: but, the lifetime must be valid for the anonymous lifetime as defined here...
  --> main.rs:10:21
   |
10 | impl<'a> DataAccess<&f64> for Container<'a> {
   |                     ^
note: ...so that the method type is compatible with trait
  --> main.rs:11:5
   |
11 |     fn get_ref(&self) -> &f64 { // Error: Lifetime mismatch
   |     ^^^^^^^^^^^^^^^^^^^^^^^^^
   = note: expected `fn(&Container<'_>) -> &_`
              found `fn(&Container<'_>) -> &_`

Real output of rustc 1.99.0 compiling the official example below (2021 edition). Your names and line numbers will differ; the first line is the part to match.

E0803Ownership and borrowing
Activein rustc 1.99.0
7related errors linked below

The compiler team's explanation

A trait implementation returns a reference without an explicit lifetime linking it to self. It commonly arises in generic trait implementations requiring explicit lifetime bounds.

Erroneous code example:

trait DataAccess<T> {
    fn get_ref(&self) -> T;
}

struct Container<'a> {
    value: &'a f64,
}

// Attempting to implement reference return
impl<'a> DataAccess<&f64> for Container<'a> {
    fn get_ref(&self) -> &f64 { // Error: Lifetime mismatch
        self.value
    }
}

The trait method returns &f64 requiring an independent lifetime The struct Container<'a> carries lifetime parameter 'a The compiler cannot verify if the returned reference satisfies 'a constraints Solution Explicitly bind lifetimes to clarify constraints:

// Modified trait with explicit lifetime binding
trait DataAccess<'a, T> {
    fn get_ref(&'a self) -> T;
}

struct Container<'a> {
    value: &'a f64,
}

// Correct implementation (bound lifetimes)
impl<'a> DataAccess<'a, &'a f64> for Container<'a> {
    fn get_ref(&'a self) -> &'a f64 {
        self.value
    }
}

From rustc --explain E0803 (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 nextE0382: borrow of moved valueA 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