I can't figure out why I need a 'static
bound in one case (bar
) and not the other (baz
):
fn f<T>(_input: T) -> bool {
false
}
fn bar<T>() -> Box<dyn Fn(T) -> bool + 'static>
where
T: 'static,
{
Box::new(f::<T>)
}
fn baz<T>() -> Box<dyn Fn(T) -> bool + 'static> {
Box::new(|x| f::<T>(x))
}
fn main() {
let f1 = bar::<&usize>();
let f2 = baz::<&usize>();
println!("Hello, world! {} {}", f1(&1), f2(&2));
}
Is this a compiler limitation? Otherwise which non-'static
type T
would break the function bar
and why?
I understand about the need for the trait object to live for the 'static
lifetime. I think I understand that the closure seems to live for 'static
as well. What I don't understand is why f::<T>: 'static
⟺ T: 'static
I looked at the function item section in the reference but its not clear to me.
async
and theFuture
s they return. I think it's just a quirk of constraint satisfaction. – Troublesomebaz
. IMObar
should compile without the constraint. – Freedwoman|x| f::<T>(x)
withf::<T>
as it normally would. It seems Clippy knows that wouldn't work here. – Freedwoman