pub trait IterNextChunk: Iterator {
    // Provided methods
    fn next_chunk<const N: usize>(
        &mut self
    ) -> Result<[Self::Item; N], IntoIter<Self::Item, N>>
       where Self: Sized { ... }
    fn next_array<const N: usize>(
        &mut self
    ) -> Result<[Self::Item; N], IntoIter<Self::Item, N>>
       where Self: Sized { ... }
}
Available on crate feature next_chunk only.
Expand description

An extension trait that provides the next_chunk method for iterators.

Note: the method provided here has a nightly API: Iterator::next_chunk.

Provided Methods§

source

fn next_chunk<const N: usize>( &mut self ) -> Result<[Self::Item; N], IntoIter<Self::Item, N>>
where Self: Sized,

Advances the iterator and returns an array containing the next N values.

If there are not enough elements to fill the array then Err is returned containing the already yielded items.

Examples

Basic usage:

use itermore::IterNextChunk;

let mut iter = "lorem".chars();

assert_eq!(iter.next_chunk().unwrap(), ['l', 'o']);      // N is inferred as 2
assert_eq!(iter.next_chunk().unwrap(), ['r', 'e', 'm']); // N is inferred as 3
assert!(iter.next_chunk::<4>().is_err());                // N is explicitly 4

Split a string and get the first three items.

use itermore::IterNextChunk;

let quote = "not all those who wander are lost";
let [first, second, third] = quote.split_whitespace().next_chunk().unwrap();
assert_eq!(first, "not");
assert_eq!(second, "all");
assert_eq!(third, "those");
source

fn next_array<const N: usize>( &mut self ) -> Result<[Self::Item; N], IntoIter<Self::Item, N>>
where Self: Sized,

Identical to next_chunk but doesn’t collide with the standard library name.

Implementors§

source§

impl<I> IterNextChunk for I
where I: Iterator + ?Sized,