Lifetime bounds with async functions

⚓ Rust    📅 2026-04-08    👤 surdeus    👁️ 4      

surdeus

I was playing with lifetimes and async functions, and encountered a compile error with the following code:

struct Invariant<'t, T>(PhantomData<fn(&'t T) -> &'t T>);

trait Rebind<'b> {
    type Output;
}

async fn test<'b, 't, T>() -> Invariant<'t, T::Output>
where
    T: Rebind<'b>,
{
    Invariant(PhantomData)
}

The compiler urges me to add T: 't and 'b: 't, but I cannot figure out why this is needed, since removing async makes the code compile.

I suppose it is due to the 2024 edition RPIT capture rules, which capture all generics by default. How do I capture the needed generics precisely?

3 posts - 3 participants

Read full topic

🏷️ Rust_feed