Closed
Description
fn count_and_sum(numbers: &Vec<u32>) -> (usize, u32) {
let mut sum: u32 = 0;
let count = numbers.iter().map(|n| sum += n).count();
(count, sum)
}
In this example, we use map
to process all values and do something with them that involves another mutable variable or function, in the end, we use count()
to get the total number of items processed.
The lint explanation says
Maybe map was confused with filter. If the map call is intentional, this should be rewritten. Or, if you intend to drive the iterator to completion, you can just use for_each instead.
But a filter is not useful here. Using for_each
is out of option since for_each().count()
does not work since for_each()
does not return anything.
I'm not sure if inspect()
is the right choice here, at least it is not noted in the lints description.