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 peek method in std::iter::Peekable.

fn peek_mut(&mut self) -> Option<&mut I::Item>

Works exactly like the peek_mut method in std::iter::Peekable.

fn peek_nth(&mut self, n: usize) -> Option<&I::Item>

Returns a reference to the nth value 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 nth value 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_if method in std::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_eq method in std::iter::Peekable.

Trait Implementations

impl<I> Clone for PeekNth<I> where I: Iterator + Clone, I::Item: Clone,

fn clone(&self) -> PeekNth<I>

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,

Auto Trait Implementations

impl<I> Freeze for PeekNth<I> where Fuse<I>: Freeze, VecDeque<<I as Iterator>::Item>: Freeze,

impl<I> RefUnwindSafe for PeekNth<I> where Fuse<I>: RefUnwindSafe, VecDeque<<I as Iterator>::Item>: RefUnwindSafe,

impl<I> Send for PeekNth<I> where Fuse<I>: Send, VecDeque<<I as Iterator>::Item>: Send,

impl<I> Sync for PeekNth<I> where Fuse<I>: Sync, VecDeque<<I as Iterator>::Item>: Sync,

impl<I> Unpin for PeekNth<I> where Fuse<I>: Unpin, VecDeque<<I as Iterator>::Item>: Unpin,

impl<I> UnsafeUnpin for PeekNth<I> where Fuse<I>: UnsafeUnpin, VecDeque<<I as Iterator>::Item>: UnsafeUnpin,

impl<I> UnwindSafe for PeekNth<I> where Fuse<I>: UnwindSafe, VecDeque<<I as Iterator>::Item>: UnwindSafe,

Blanket Implementations

impl<I> IntoIterator for PeekNth<I> where I: Iterator,

type Item = <I as Iterator>::Item;
type IntoIter = I;
fn into_iter(self) -> I

impl<IT> MultiUnzip<()> for PeekNth<I> where IT: Iterator<Item = ()>,

fn multiunzip(self)

impl<IT, A, FromA> MultiUnzip<(FromA,)> for PeekNth<I> where IT: Iterator<Item = (A,)>, FromA: Default + Extend<A>,

fn multiunzip(self) -> (FromA,)

impl<IT, A, FromA, B, FromB> MultiUnzip<(FromA, FromB)> for PeekNth<I> where IT: Iterator<Item = (A, B)>, FromA: Default + Extend<A>, FromB: Default + Extend<B>,

fn multiunzip(self) -> (FromA, FromB)

impl<IT, A, FromA, B, FromB, C, FromC> MultiUnzip<(FromA, FromB, FromC)> for PeekNth<I> where IT: Iterator<Item = (A, B, C)>, FromA: Default + Extend<A>, FromB: Default + Extend<B>, FromC: Default + Extend<C>,

fn multiunzip(self) -> (FromA, FromB, FromC)

impl<IT, A, FromA, B, FromB, C, FromC, D, FromD> MultiUnzip<(FromA, FromB, FromC, FromD)> for PeekNth<I> where IT: Iterator<Item = (A, B, C, D)>, FromA: Default + Extend<A>, FromB: Default + Extend<B>, FromC: Default + Extend<C>, FromD: Default + Extend<D>,

fn multiunzip(self) -> (FromA, FromB, FromC, FromD)

impl<IT, A, FromA, B, FromB, C, FromC, D, FromD, E, FromE> MultiUnzip<(FromA, FromB, FromC, FromD, FromE)> for PeekNth<I> where IT: Iterator<Item = (A, B, C, D, E)>, FromA: Default + Extend<A>, FromB: Default + Extend<B>, FromC: Default + Extend<C>, FromD: Default + Extend<D>, FromE: Default + Extend<E>,

fn multiunzip(self) -> (FromA, FromB, FromC, FromD, FromE)

impl<IT, A, FromA, B, FromB, C, FromC, D, FromD, E, FromE, F, FromF> MultiUnzip<(FromA, FromB, FromC, FromD, FromE, FromF)> for PeekNth<I> where IT: Iterator<Item = (A, B, C, D, E, F)>, FromA: Default + Extend<A>, FromB: Default + Extend<B>, FromC: Default + Extend<C>, FromD: Default + Extend<D>, FromE: Default + Extend<E>, FromF: Default + Extend<F>,

fn multiunzip(self) -> (FromA, FromB, FromC, FromD, FromE, FromF)

impl<IT, A, FromA, B, FromB, C, FromC, D, FromD, E, FromE, F, FromF, G, FromG> MultiUnzip<(FromA, FromB, FromC, FromD, FromE, FromF, FromG)> for PeekNth<I> where IT: Iterator<Item = (A, B, C, D, E, F, G)>, FromA: Default + Extend<A>, FromB: Default + Extend<B>, FromC: Default + Extend<C>, FromD: Default + Extend<D>, FromE: Default + Extend<E>, FromF: Default + Extend<F>, FromG: Default + Extend<G>,

fn multiunzip(self) -> (FromA, FromB, FromC, FromD, FromE, FromF, FromG)

impl<IT, A, FromA, B, FromB, C, FromC, D, FromD, E, FromE, F, FromF, G, FromG, H, FromH> MultiUnzip<(FromA, FromB, FromC, FromD, FromE, FromF, FromG, FromH)> for PeekNth<I> where IT: Iterator<Item = (A, B, C, D, E, F, G, H)>, FromA: Default + Extend<A>, FromB: Default + Extend<B>, FromC: Default + Extend<C>, FromD: Default + Extend<D>, FromE: Default + Extend<E>, FromF: Default + Extend<F>, FromG: Default + Extend<G>, FromH: Default + Extend<H>,

fn multiunzip(self) -> (FromA, FromB, FromC, FromD, FromE, FromF, FromG, FromH)

impl<IT, A, FromA, B, FromB, C, FromC, D, FromD, E, FromE, F, FromF, G, FromG, H, FromH, I, FromI> MultiUnzip<(FromA, FromB, FromC, FromD, FromE, FromF, FromG, FromH, FromI)> for PeekNth<I> where IT: Iterator<Item = (A, B, C, D, E, F, G, H, I)>, FromA: Default + Extend<A>, FromB: Default + Extend<B>, FromC: Default + Extend<C>, FromD: Default + Extend<D>, FromE: Default + Extend<E>, FromF: Default + Extend<F>, FromG: Default + Extend<G>, FromH: Default + Extend<H>, FromI: Default + Extend<I>,

fn multiunzip(self) -> (FromA, FromB, FromC, FromD, FromE, FromF, FromG, FromH, FromI)

impl<IT, A, FromA, B, FromB, C, FromC, D, FromD, E, FromE, F, FromF, G, FromG, H, FromH, I, FromI, J, FromJ> MultiUnzip<(FromA, FromB, FromC, FromD, FromE, FromF, FromG, FromH, FromI, FromJ)> for PeekNth<I> where IT: Iterator<Item = (A, B, C, D, E, F, G, H, I, J)>, FromA: Default + Extend<A>, FromB: Default + Extend<B>, FromC: Default + Extend<C>, FromD: Default + Extend<D>, FromE: Default + Extend<E>, FromF: Default + Extend<F>, FromG: Default + Extend<G>, FromH: Default + Extend<H>, FromI: Default + Extend<I>, FromJ: Default + Extend<J>,

fn multiunzip(self) -> (FromA, FromB, FromC, FromD, FromE, FromF, FromG, FromH, FromI, FromJ)

impl<IT, A, FromA, B, FromB, C, FromC, D, FromD, E, FromE, F, FromF, G, FromG, H, FromH, I, FromI, J, FromJ, K, FromK> MultiUnzip<(FromA, FromB, FromC, FromD, FromE, FromF, FromG, FromH, FromI, FromJ, FromK)> for PeekNth<I> where IT: Iterator<Item = (A, B, C, D, E, F, G, H, I, J, K)>, FromA: Default + Extend<A>, FromB: Default + Extend<B>, FromC: Default + Extend<C>, FromD: Default + Extend<D>, FromE: Default + Extend<E>, FromF: Default + Extend<F>, FromG: Default + Extend<G>, FromH: Default + Extend<H>, FromI: Default + Extend<I>, FromJ: Default + Extend<J>, FromK: Default + Extend<K>,

fn multiunzip(self) -> (FromA, FromB, FromC, FromD, FromE, FromF, FromG, FromH, FromI, FromJ, FromK)

impl<IT, A, FromA, B, FromB, C, FromC, D, FromD, E, FromE, F, FromF, G, FromG, H, FromH, I, FromI, J, FromJ, K, FromK, L, FromL> MultiUnzip<(FromA, FromB, FromC, FromD, FromE, FromF, FromG, FromH, FromI, FromJ, FromK, FromL)> for PeekNth<I> where IT: Iterator<Item = (A, B, C, D, E, F, G, H, I, J, K, L)>, FromA: Default + Extend<A>, FromB: Default + Extend<B>, FromC: Default + Extend<C>, FromD: Default + Extend<D>, FromE: Default + Extend<E>, FromF: Default + Extend<F>, FromG: Default + Extend<G>, FromH: Default + Extend<H>, FromI: Default + Extend<I>, FromJ: Default + Extend<J>, FromK: Default + Extend<K>, FromL: Default + Extend<L>,

fn multiunzip(self) -> (FromA, FromB, FromC, FromD, FromE, FromF, FromG, FromH, FromI, FromJ, FromK, FromL)

impl<T> Any for PeekNth<I> where T: 'static + ?Sized,

fn type_id(&self) -> TypeId

impl<T> Borrow<T> for PeekNth<I> where T: ?Sized,

fn borrow(&self) -> &T

impl<T> BorrowMut<T> for PeekNth<I> where T: ?Sized,

fn borrow_mut(&mut self) -> &mut T

impl<T> CloneToUninit for PeekNth<I> where T: Clone,

unsafe fn clone_to_uninit(&self, dest: *mut u8)

impl<T> From<T> for PeekNth<I>

fn from(t: T) -> T

Returns the argument unchanged.

impl<T> IntoEither for PeekNth<I>

impl<T> Itertools for PeekNth<I> where T: Iterator + ?Sized,

impl<T> ToOwned for PeekNth<I> where T: Clone,

type Owned = T;
fn to_owned(&self) -> T
fn clone_into(&self, target: &mut T)

impl<T, U> Into<U> for PeekNth<I> where U: From<T>,

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of [From]<T> for U chooses to do.

impl<T, U> TryFrom<U> for PeekNth<I> where U: Into<T>,

type Error = never;
fn try_from(value: U) -> Result<T, never>

impl<T, U> TryInto<U> for PeekNth<I> where U: TryFrom<T>,

type Error = <U as TryFrom<T>>::Error;
fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>