#iterator #iterator-adapter #peek

peeking-iter

An iterator adapter that allows infinitely-deep peeking

4 releases

0.2.0 Jul 11, 2024
0.1.2 May 27, 2024
0.1.1 May 27, 2024
0.1.0 May 27, 2024

#685 in Rust patterns

MIT license

13KB
161 lines

peeking-iter

A fast and simple iterator adapter that allows peeking with any depth:

use peeking_iter::PeekingIter

fn main() {
    let it = PeekingIter::new(0..=2);

    assert_eq!(it.next(), Some(0));
    assert_eq!(it.peek(), Some(1));
    assert_eq!(it.peek(), Some(2));
    assert_eq!(it.next(), Some(1));
    assert_eq!(it.peek(), Some(2));
    assert_eq!(it.peek(), None);
}

Why?

  • ~1.5x faster than itertools' MultiPeek
  • Has useful methods initially designed with lexing and parsing in mind (see Parser)

No runtime deps