std-method

Iterator::peekable — look ahead

Wrap an iterator to inspect the next item without removing it from the wrapper. peek may pull and cache an item from the underlying iterator. Expected stdout (not executed): Some("-v") Some("-v") Some("input.txt") Common mistake: Assuming peek cannot cause any side effect in the underlying source.

Formal Syntax & Structure

rust syntaxrust
fn peekable(self) -> Peekable<Self> where Self: Sized

Working Example

Example Snippetrust
fn main() {    let mut tokens = ["-v", "input.txt"].into_iter().peekable();    println!("{:?}", tokens.peek());    println!("{:?}", tokens.next());    println!("{:?}", tokens.next());}
Rust Reference