Email me

Rust errors / Ownership and borrowing

Rust compiler error · checked with rustc 1.99.0

Rust error E0309: the parameter type `T` may not live long enough

What it meansA parameter type is missing an explicit lifetime bound and may not live long enough.

What rustc prints

error[E0309]: the parameter type `T` may not live long enough
 --> main.rs:5:10
  |
4 | struct Foo<'a, T> {
  |            -- the parameter type `T` must be valid for the lifetime `'a` as defined here...
5 |     foo: <T as SomeTrait<'a>>::Output,
  |          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ...so that the type `T` will meet its required lifetime bounds
  |
help: consider adding an explicit lifetime bound
  |
4 | struct Foo<'a, T: '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.

E0309Ownership and borrowing
Activein rustc 1.99.0
8related errors linked below

The compiler team's explanation

A parameter type is missing an explicit lifetime bound and may not live long enough.

Erroneous code example:

// This won't compile because the applicable impl of
// `SomeTrait` (below) requires that `T: 'a`, but the struct does
// not have a matching where-clause.
struct Foo<'a, T> {
    foo: <T as SomeTrait<'a>>::Output,
}

trait SomeTrait<'a> {
    type Output;
}

impl<'a, T> SomeTrait<'a> for T
where
    T: 'a,
{
    type Output = u32;
}

The type definition contains some field whose type requires an outlives annotation. Outlives annotations (e.g., T: 'a) are used to guarantee that all the data in T is valid for at least the lifetime 'a. This scenario most commonly arises when the type contains an associated type reference like <T as SomeTrait<'a>>::Output, as shown in the previous code.

There, the where clause T: 'a that appears on the impl is not known to be satisfied on the struct. To make this example compile, you have to add a where-clause like T: 'a to the struct definition:

struct Foo<'a, T>
where
    T: 'a,
{
    foo: <T as SomeTrait<'a>>::Output
}

trait SomeTrait<'a> {
    type Output;
}

impl<'a, T> SomeTrait<'a> for T
where
    T: 'a,
{
    type Output = u32;
}

From rustc --explain E0309 (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