The peekable_next
crate provides an extension for Rust iterators, introducing the PeekableNext
struct and associated traits. This extension allows users to peek at the next element of an iterator without advancing it, which can be valuable in various scenarios where you need to inspect upcoming values before making further decisions.
Add this crate to your Cargo.toml
:
```toml [dependencies] peekable_next = "0.1.1"
Import the PeekNext
trait into your code:
rust
use peekable_next::PeekNext;
```rust let data = vec![1, 2, 3]; let mut iter = data.iter().peekable_next();
// Peeking allows us to see the next value without advancing the iterator asserteq!(iter.peek(), Some(&&1)); asserteq!(iter.peek(), Some(&&1)); assert_eq!(iter.next(), Some(&1)); // Advances to the next element (2)
asserteq!(iter.peek(), Some(&&2)); asserteq!(iter.next(), Some(&2)); // Advances to the next element (3)
// After the iterator is finished, peek returns None asserteq!(iter.peek(), None); asserteq!(iter.next(), None); ```