This is a well-known limitation of closures: they cannot satisfy a for<'a> Trait<'a>
bound where Trait<'a>
requires Fn(&'a T)
. Here, the compiler can infer that the type of f
implements Function<'a, String, (&'a str,)>
for any particular lifetime 'a
. This allows the explicit Function::call_func(&f, args)
in main()
to work with no issues. However, f
cannot be passed to takes_any_function()
, since f
's type does not implement for<'a> Function<'a, String, (&'a str,)>
. Currently, the only workaround is to use a lifetime-generic fn
item instead of a closure. See also RFC 3216 for a proposed language solution to this issue.