usecrate::size_hint; usecrate::PeekingNext; use alloc::collections::VecDeque; use std::iter::Fuse;
/// See [`peek_nth()`] for more information. #[derive(Clone, Debug)] pubstruct PeekNth<I> where
I: Iterator,
{
iter: Fuse<I>,
buf: VecDeque<I::Item>,
}
/// A drop-in replacement for [`std::iter::Peekable`] which adds a `peek_nth` /// method allowing the user to `peek` at a value several iterations forward /// without advancing the base iterator. /// /// This differs from `multipeek` in that subsequent calls to `peek` or /// `peek_nth` will always return the same value until `next` is called /// (making `reset_peek` unnecessary). pubfn peek_nth<I>(iterable: I) -> PeekNth<I::IntoIter> where
I: IntoIterator,
{
PeekNth {
iter: iterable.into_iter().fuse(),
buf: VecDeque::new(),
}
}
impl<I> PeekNth<I> where
I: Iterator,
{ /// Works exactly like the `peek` method in `std::iter::Peekable` pubfn peek(&mutself) -> Option<&I::Item> { self.peek_nth(0)
}
/// Returns a reference to the `nth` value without advancing the iterator. /// /// # Examples /// /// Basic usage: /// /// ```rust /// use itertools::peek_nth; /// /// let xs = vec![1,2,3]; /// let mut iter = peek_nth(xs.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); /// ``` pubfn peek_nth(&mutself, n: usize) -> Option<&I::Item> { let unbuffered_items = (n + 1).saturating_sub(self.buf.len());
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.