Rust errors / Ownership and borrowing
Rust compiler error · checked with rustc 1.99.0
Rust error E0311: the parameter type `T` may not live long enough
What it meansThis error occurs when there is an unsatisfied outlives bound involving an elided region and a generic type parameter or associated type.
What rustc prints
error[E0311]: the parameter type `T` may not live long enough --> main.rs:2:5 | 1 | fn no_restriction<T>(x: &()) -> &() { | --- the parameter type `T` must be valid for the anonymous lifetime defined here... 2 | with_restriction::<T>(x) | ^^^^^^^^^^^^^^^^^^^^^ ...so that the type `T` will meet its required lifetime bounds | help: consider adding an explicit lifetime bound | 1 | fn no_restriction<'a, T: 'a>(x: &'a ()) -> &'a () { | +++ ++++ ++ ++
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
This error occurs when there is an unsatisfied outlives bound involving an elided region and a generic type parameter or associated type.
Erroneous code example:
fn no_restriction<T>(x: &()) -> &() {
with_restriction::<T>(x)
}
fn with_restriction<'a, T: 'a>(x: &'a ()) -> &'a () {
x
}
Why doesn't this code compile? It helps to look at the lifetime bounds that are automatically added by the compiler. For more details see the documentation for [lifetime elision]( https://doc.rust-lang.org/reference/lifetime-elision.html).
The compiler elides the lifetime of x and the return type to some arbitrary lifetime 'anon in no_restriction(). The only information available to the compiler is that 'anon is valid for the duration of the function. When calling with_restriction(), the compiler requires the completely unrelated type parameter T to outlive 'anon because of the T: 'a bound in with_restriction(). This causes an error because T is not required to outlive 'anon in no_restriction().
If no_restriction() were to use &T instead of &() as an argument, the compiler would have added an implied bound, causing this to compile.
This error can be resolved by explicitly naming the elided lifetime for x and then explicitly requiring that the generic parameter T outlives that lifetime:
fn no_restriction<'a, T: 'a>(x: &'a ()) -> &'a () {
with_restriction::<T>(x)
}
fn with_restriction<'a, T: 'a>(x: &'a ()) -> &'a () {
x
}From rustc --explain E0311 (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
- E0309the parameter type `T` may not live long enough
- E0310the parameter type may not live long enough
- E0316nested quantification of lifetimes
- E0373closure may outlive the current function, but it borrows
Up next
Often hit nextE0382: borrow of moved valueA related error from the same family.Read it next →Did this answer your question?