Struct PeekNth
#[must_use = "iterator adaptors are lazy and do nothing unless consumed"]
pub struct PeekNth<I>
where
I: Iterator, { /* private fields */ }
See [peek_nth()] for more information.
Implementations
impl<I> PeekNth<I>
where
I: Iterator,
fn peek(&mut self) -> Option<&I::Item>Works exactly like the
peekmethod instd::iter::Peekable.fn peek_mut(&mut self) -> Option<&mut I::Item>Works exactly like the
peek_mutmethod instd::iter::Peekable.fn peek_nth(&mut self, n: usize) -> Option<&I::Item>Returns a reference to the
nthvalue without advancing the iterator.Examples
Basic usage:
use itertools::peek_nth; let xs = vec![1, 2, 3]; let mut iter = peek_nth(xs.into_iter()); assert_eq!(iter.peek_nth(0), Some(&1)); assert_eq!(iter.next(), Some(1)); // The iterator does not advance even if we call `peek_nth` multiple times assert_eq!(iter.peek_nth(0), Some(&2)); assert_eq!(iter.peek_nth(1), Some(&3)); assert_eq!(iter.next(), Some(2)); // Calling `peek_nth` past the end of the iterator will return `None` assert_eq!(iter.peek_nth(1), None);fn peek_nth_mut(&mut self, n: usize) -> Option<&mut I::Item>Returns a mutable reference to the
nthvalue without advancing the iterator.Examples
Basic usage:
use itertools::peek_nth; let xs = vec![1, 2, 3, 4, 5]; let mut iter = peek_nth(xs.into_iter()); assert_eq!(iter.peek_nth_mut(0), Some(&mut 1)); assert_eq!(iter.next(), Some(1)); // The iterator does not advance even if we call `peek_nth_mut` multiple times assert_eq!(iter.peek_nth_mut(0), Some(&mut 2)); assert_eq!(iter.peek_nth_mut(1), Some(&mut 3)); assert_eq!(iter.next(), Some(2)); // Peek into the iterator and set the value behind the mutable reference. if let Some(p) = iter.peek_nth_mut(1) { assert_eq!(*p, 4); *p = 9; } // The value we put in reappears as the iterator continues. assert_eq!(iter.next(), Some(3)); assert_eq!(iter.next(), Some(9)); // Calling `peek_nth_mut` past the end of the iterator will return `None` assert_eq!(iter.peek_nth_mut(1), None);fn next_if(&mut self, func: impl FnOnce(&I::Item) -> bool) -> Option<I::Item>Works exactly like the
next_ifmethod instd::iter::Peekable.fn next_if_eq<T>(&mut self, expected: &T) -> Option<I::Item> where T: ?Sized, I::Item: PartialEq<T>,Works exactly like the
next_if_eqmethod instd::iter::Peekable.
Trait Implementations
impl<I> Clone for PeekNth<I>
where
I: Iterator + Clone,
I::Item: Clone,
fn clone(&self) -> Self
impl<I> Debug for PeekNth<I>
where
I: Iterator + Debug,
I::Item: Debug,
fn fmt(&self, f: &mut Formatter<'_>) -> Result
impl<I> ExactSizeIterator for PeekNth<I>
where
I: ExactSizeIterator,
impl<I> Iterator for PeekNth<I>
where
I: Iterator,
type Item = <I as Iterator>::Item;fn next(&mut self) -> Option<Self::Item>fn size_hint(&self) -> (usize, Option<usize>)fn fold<B, F>(self, init: B, f: F) -> B where F: FnMut(B, Self::Item) -> B,
impl<I> PeekingNext for PeekNth<I>
where
I: Iterator,
fn peeking_next<F>(&mut self, accept: F) -> Option<Self::Item> where F: FnOnce(&Self::Item) -> bool,