Signature

pub trait PeekableTrait+Copy+Drop>

Trait functions

peek

Returns a copy of the next() value without advancing the iterator. Like next, if there is a value, it is wrapped in a Some(T). But if the iteration is over, None is returned.

Signature

fn peek,
    +Copy,
    +Drop,
    I,
    impl IterI: Iterator,
    +Copy,
    +Drop,
>(
    ref self: Peekable,
) -> Option

Examples

Basic usage:
let mut iter = (1..4_u8).into_iter().peekable();

// peek() lets us see one step into the future
assert_eq!(iter.peek(), Some(1));
assert_eq!(iter.next(), Some(1));

assert_eq!(iter.next(), Some(2));

// The iterator does not advance even if we `peek` multiple times
assert_eq!(iter.peek(), Some(3));
assert_eq!(iter.peek(), Some(3));

assert_eq!(iter.next(), Some(3));

// After the iterator is finished, so is `peek()`
assert_eq!(iter.peek(), None);
assert_eq!(iter.next(), None);