If I have the following code:
let a = 1;
let f = |n| n == &a;
let _: Vec<_> = (1u64..10).filter(f).collect();
Rust complains greatly that collect exists for the relevant Filter struct, but the trait bound FnMut is not satisfied by the closure.
However, if I inline the closure or annotate its argument type, the code works, such as:
let a = 1;
let _: Vec<_> = (1u64..10).filter(|n| n == &a).collect();
or:
let a = 1;
let f = |n: &u64| n == &a;
let _: Vec<_> = (1u64..10).filter(f).collect();
Why is this? The fact that inlining the closure without annotating the type works is truly bizarre. I would think it was because n was having its type inferred as u64 instead of &u64 because ranges have some kind of propensity towards getting consumed but I don't know.