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.
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
- E0382borrow of moved value
- E0499cannot borrow as mutable more than once at a time
- E0502cannot borrow as mutable because it is also borrowed as immutable
- E0106missing lifetime specifier
- E0747type provided when a lifetime was expected
- E0764mutable borrows of temporaries that have their lifetime extended until the end of the program are not allowed
- E0794cannot specify lifetime arguments explicitly if late bound lifetime parameters are present
Up next
Often hit nextE0382: borrow of moved valueA related error from the same family.Read it next →Did this answer your question?