How to for_each and then count them in Rust?

Viewed 8

I'm trying to go through an iterator, select certain elements, do something with each of them and count how many of them were affected:

let done = foo
  .into_iter()
  .filter(...)
  .for_each(|i| do_something_with(i))
  .len();

This doesn't work since for_each doesn't return an iterator. The best option I found so far is this:

let mut done = 0;
foo
  .into_iter()
  .filter(...)
  .for_each(|i| { do_something_with(i); done += 1; });

Maybe there is a more elegant immutable one?

0 Answers
Related