I am looping over an iterator and inside the loop I would occasionally like to skip forward a a few steps. But something like skip() won't work since it apparently takes ownership of the iterator. Is there some easy way of skipping forward a bunch of steps in the iteration without having to call next() lots of times in a row?
Code would be something like:
let mut buffer: Vec<u8> = // Some data
let mut buff_iter = buffer.into_iter();
while let Some(x) = buff_iter.next() {
// Do lost of stuff
// If some condition is true
buff_iter.skip(20);
}
I'd just like to add to @stebalien's answer, you should substract one from the number of items to skip to use nth, i.e. iter.nth(0) to skip one item, iter.nth(1) to skip two etc. Just to avoid a catch here.