In the code here
trait Foo {
type Output;
fn foo(self) -> Self::Output;
}
impl<'a> Foo for &'a () {
type Output = &'a ();
fn foo(self) -> Self::Output {
self
}
}
fn func<F: Foo>(f: F) -> F::Output {
f.foo()
}
fn func2<'a>(f: &'a ()) -> &'a () {
func::<&'a ()>(f)
}
fn has_hrl<F: Fn(&()) -> &()>(f: F) {}
fn main() {
//has_hrl(func); // FAILS
has_hrl(func2);
has_hrl(|x| func(x));
}
We would like to do has_hrl(func), but Rust only accepts the closure has_hrl(|x| func(x)). Why is that? Because it works with concrete types like in func2, but not with generic types.
has_hrllikefn has_hrl<'a, F: Fn(&'a ()) -> &'a ()>(_: F) {}. My power level is insufficient to explain why the lifetime needs to be explicit, though. – ljedrz Sep 18 '16 at 16:56