I have a long iterator chain generating data that is inserted into a fixed array with for_each. An MWE version (playground):
fn f(xs: impl Iterator<Item = u8>) { let mut buf = [0u8; 32]; xs.zip(buf.iter_mut()).for_each(|(v, b)| *b = v); // ... process buf } I'd like to count how many values were inserted. A mut variable that is updated in for_each is one possibility, but I wanted to see if mut can be avoided. Tried the following:
fn g(xs: impl Iterator<Item = u8>) { let mut buf = [0u8; 32]; let c = xs.zip(buf.iter_mut()).inspect(|(v, b)| *b = v).count(); // ... process buf } but this does not work because I cannot modify b inside of inspect.
Is there a way for the iterator chain to do the array assignment and return the count?