I\'ve constructed a closure example that I can\'t get to work, nor can I find any reason why it shouldn\'t work. Why does it fail to compile on the last closure?
Pl
From following Type mismatches resolving a closure that takes arguments by reference and How to declare a lifetime for a closure argument? it appears the solution is to change:
fn filter(predicate: P)
where
P: Fn(&S) -> bool,
{
predicate(&S {});
}
to
fn filter<'a, P>(predicate: P)
where
P: Fn(&'a S) -> bool,
{
predicate(&S {});
}
Though I'm not sure why. It seems to be related to inferred lifetimes when a closure is specified inline vs when it is stored in a variable and used later. But it's unclear why &S needs an 'a lifetime, when &S is not a result that is returned. If you understand this, please explain in a comment.
Though this question is "solved", the trimmed-down failure case posted originally does not actually help my true problem, because I cannot edit the source of the code I am having trouble with https://docs.rs/walkdir/2.2.9/walkdir/struct.IntoIter.html#method.filter_entry
The issue manifested when I tried to pass a stored callback into the filter_entry method. The solution would be to put in explicit lifetimes in the filter_entry signature, as described earlier in this post, but you can only do that if you want to edit the third party code. I think unfortunately the answer for that particular problem is "you can't use a stored closure with filter_entry"