Create an iterator that can peek at the next element.
The peekable method wraps an iterator in a Peekable adapter, which adds a peek method that returns a reference to the next element without consuming it.
Peekable is useful when you need to make decisions based on upcoming elements, such as in parsers, tokenizers, and look-ahead algorithms. The peeked element is cached and returned on the next call to next.
peek returns an Option<&Item>, and calling next after peek returns the same element. The peek_mut method allows modifying the peeked value in place.
let mut iter = vec![1, 2, 3].into_iter().peekable();
assert_eq!(iter.peek(), Some(&1));
assert_eq!(iter.next(), Some(1));
assert_eq!(iter.peek(), Some(&2));