Better Peekable
A no_std compatible iterator adapter that lets you peek multiple items ahead - perfect for building lexers, parsers, and other lookahead-heavy code.
Unlike std::iter::Peekable which only lets you peek one item ahead, BetterPeekable provides peek_n(n) to look arbitrarily far into your iterator without consuming items. Of course it also provides peek() which is the same as peek_n(0).
Installation
For no_std environments:
Quick Example
use BetterPeekable;
let mut tokens = "if x == 42".chars.better_peekable;
// Look ahead to parse keywords
if tokens.peek == Some && tokens.peek_n == Some
Lexer Example
Here's how you might use it to build a simple lexer that needs lookahead:
use BetterPeekable;
More Examples
For a complete lexer/parser example with advanced lookahead patterns, see examples/lexer.rs.
Run it with:
Key Features
- Multi-item lookahead:
peek_n(n)lets you looknitems ahead - Idempotent: Multiple
peekcalls don't affect the iterator state - Full iterator support: Works with
DoubleEndedIterator,ExactSizeIterator, etc. no_stdcompatible: Works in embedded and WASM environments withalloc