Crates.io | multipeek |
lib.rs | multipeek |
version | 0.1.2 |
source | src |
created_at | 2022-04-17 17:52:10.187561 |
updated_at | 2022-04-17 20:26:56.156102 |
description | An adapter to peek at future elements in an iterator without advancing the iterator cursor |
homepage | |
repository | https://github.com/LukeMathWalker/multipeek |
max_upload_size | |
id | 569522 |
size | 29,189 |
An iterator adapter to peek at future elements without advancing the cursor of the underlying iterator.
Check out the documentation for more details.
use multipeek::multipeek;
let mut iter = multipeek([1, 2, 3, 4].into_iter());
// Peek at the first element.
let first_peek = iter.peek().cloned();
assert_eq!(first_peek, Some(1));
// Advance the iterator cursor to point at the first element.
let first = iter.next();
assert_eq!(first, first_peek);
// Peek two steps ahead, at the third element.
let third_peek = iter.peek_nth(1).cloned();
assert_eq!(third_peek, Some(3));
// Advance the iterator cursor twice.
// The iterator cursor will now point to the third element.
iter.next();
let third = iter.next();
assert_eq!(third_peek, third);
// Peeking beyond the end of the iterator returns `None`.
let ambitious_peek = iter.peek_nth(5);
assert!(ambitious_peek.is_none());
no_std
multipeek
can be used in no_std
environments. It requires an allocator.
Rust's standard library provides Peekable
.
It lets you peek at the next element in an iterator, but there is no way to look further ahead.
itertools
's provides MultiPeek
.
It lets you peek as far ahead as you want, but MultiPeek::peek
is not idempotent: calling peek
once returns the next element, calling peek
again
returns the second-next element.
multipeek
, just like itertools
, gives you the possibility to peek as far ahead as you want.
Our MultiPeek::peek
implementation is idempotent: MultiPeek::peek
always returns
the next element.
You can peek further ahead using MultiPeek::peek_nth
, you just need to specify how
many steps ahead you want to look at.
Our MultiPeek
implementation is directly inspired by itertools
' implementation.
Licensed under either of Apache License, Version 2.0 or MIT license at your option. Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in this crate by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.