Is there an impl for trait<'a> with any lifetime?

I found the code, which compiles, the T: for<'b> A<'b> confuses me, from the output of rustc, I got to know it means some T which implements trait A with any lifetime.

Is there something can satisfy the bound indeed?

#![allow(unused_variables)]

trait A<'a> {
    fn from_i(a: &'a i32) -> Self;
}

fn test<T>()
where
    T: for<'b> A<'b>,
{}

fn main() {

}

Sure.

any type that doesn't have a lifetime parameter or only contains 'static will do. for example:

impl<'a> A<'a> for i64 {
    fn from_i(_: &'a i32) -> Self {
        42
    }
}

and

impl A<'_> for &'static str {
    fn from_i(i:&i32) -> Self {
        "hello"
    }
}

There's no 'static requirement on the implementor.

In more nuanced cases, implied bounds can result in a higher ranked trait bound (HRTB) being true even when the binder lifetime has an (implied) constraint.

2 Likes

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.