Crate peek_nth

Source
Expand description

An iterator adapter that allows you to efficiently peek the nth item of an iterator.

Itermediate values are memoized and heap allocations are avoided when possible.

§Usage

extern crate peek_nth;

use peek_nth::IteratorExt;

fn main() {
    let mut iter = "Hello, world!".chars().peekable_nth();

    assert_eq!(iter.peek_nth(4), Some(&'o'));
    assert_eq!(iter.peek_nth(3), Some(&'l'));
    assert_eq!(iter.peek_nth(2), Some(&'l'));
    assert_eq!(iter.peek_nth(1), Some(&'e'));
    assert_eq!(iter.peek_nth(0), Some(&'H'));
    assert_eq!(iter.peek_nth(7), Some(&'w'));
    assert_eq!(iter.collect::<String>(), "Hello, world!");
}

Structs§

PeekableNth
An iterator with a peek_nth() method that returns an optional reference to the nth element.

Traits§

IteratorExt
Adds a peekable_nth() method to types that implement std::iter::Iterator.