Trait itermore::IterChunks
source · pub trait IterChunks: Iterator {
fn next_chunk<const N: usize>(&mut self) -> Option<[Self::Item; N]>
where
Self: Sized,
{ ... }
fn next_array<const N: usize>(&mut self) -> Option<[Self::Item; N]>
where
Self: Sized,
{ ... }
fn chunks<const N: usize>(self) -> Chunks<Self, N> ⓘ
where
Self: Sized,
{ ... }
fn chunked<const N: usize>(self) -> Chunks<Self, N> ⓘ
where
Self: Sized,
{ ... }
}Expand description
An extension trait that provides the chunks
method for iterators.
Provided Methods§
sourcefn next_chunk<const N: usize>(&mut self) -> Option<[Self::Item; N]>where
Self: Sized,
fn next_chunk<const N: usize>(&mut self) -> Option<[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 None is
returned.
Examples
Basic usage:
use iterchunks::IterChunks;
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_none()); // N is explicitly 4Split a string and get the first three items.
use iterchunks::IterChunks;
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");sourcefn next_array<const N: usize>(&mut self) -> Option<[Self::Item; N]>where
Self: Sized,
fn next_array<const N: usize>(&mut self) -> Option<[Self::Item; N]>where
Self: Sized,
Identical to next_chunk but doesn’t collide
with the standard library name.
sourcefn chunks<const N: usize>(self) -> Chunks<Self, N> ⓘwhere
Self: Sized,
fn chunks<const N: usize>(self) -> Chunks<Self, N> ⓘwhere
Self: Sized,
Returns an iterator over N elements of the iterator at a time.
The chunks do not overlap. If N does not divide the length of the
iterator, then the last up to N-1 elements will be omitted.
Panics
If called with N = 0.
Examples
Basic usage:
use iterchunks::IterChunks;
let mut iter = "lorem".chars().chunks();
assert_eq!(iter.next(), Some(['l', 'o']));
assert_eq!(iter.next(), Some(['r', 'e']));
assert_eq!(iter.next(), None);use iterchunks::IterChunks;
let data = [1, 1, 2, -2, 6, 0, 3, 1];
// ^-----^ ^------^
for [x, y, z] in data.iter().chunks() {
assert_eq!(x + y + z, 4);
}