#iterator #peek #numbers #stacked #peekable #element #peeking

duck

Like Iterator::peekable, but can be stacked an arbitrary number of times

2 unstable releases

Uses old Rust 2015

0.2.0 Jul 23, 2017
0.1.0 Jul 18, 2017

#8 in #stacked

MIT/Apache

9KB
145 lines

duck

It's a peeking duck... geddit?


lib.rs:

This crate exposes a .peeking() method, for looking at the next element in an iterator without consuming it.

Unlike the peekable method in the standard library, peeking can be stacked. This means that chaining multiple calls of peeking will increase the number of elements returned by peek.

Note that this library will .clone() the elements that are peeked. While this is not strictly necessary, it does make the implementation much easier to understand, especially given the lack of generic associated types in current Rust.

Example

let anatids = vec!["duck", "goose", "swan"];

// Since .peeking() is called twice, this iterator will peek two elements ahead
let mut iter = anatids.into_iter().peeking().peeking();
assert_eq!(iter.peek(), Some(("duck", Some("goose"))));

// Step the iterator twice
iter.next(); iter.next();

// Now we're at "swan", which has no elements after it
assert_eq!(iter.peek(), Some(("swan", None)));

No runtime deps