Reputation: 2368
When using filter
, you receive a reference because filter
does not return ownership. However iter()
already references what you are iterating over so you get code that looks like this:
fn main() {
let mut vec: Vec<(bool, i32)> = Vec::new();
vec.push((true, 1));
vec.push((false, 2));
vec.push((true, 3));
for &(_, x) in vec.iter().filter(|&&(exists, _)| exists) {
println!("{}", x);
}
}
That seems like a lot of &
to me. Is this considered the idiomatic way to implement filter()
?
Upvotes: 1
Views: 76