pub trait IterArrayChunks: Iterator {
// Provided methods
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 array_chunks<const N: usize>(self) -> ArrayChunks<Self, N> ⓘ
where Self: Sized { ... }
fn array_chunked<const N: usize>(self) -> ArrayChunks<Self, N> ⓘ
where Self: Sized { ... }
}
Expand description
An extension trait that provides the next_chunk
and array_chunks
methods for iterators.
The methods provided here have the corresponding nightly APIs:
The nightly APIs handle remainders better and will likely have better performance, so they should be preferred if possible.
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 itermore::IterArrayChunks;
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 4
Split a string and get the first three items.
use itermore::IterArrayChunks;
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 array_chunks<const N: usize>(self) -> ArrayChunks<Self, N> ⓘwhere
Self: Sized,
fn array_chunks<const N: usize>(self) -> ArrayChunks<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 itermore::IterArrayChunks;
let mut iter = "lorem".chars().array_chunks();
assert_eq!(iter.next(), Some(['l', 'o']));
assert_eq!(iter.next(), Some(['r', 'e']));
assert_eq!(iter.next(), None);
use itermore::IterArrayChunks;
let data = [1, 1, 2, -2, 6, 0, 3, 1];
// ^-----^ ^------^
for [x, y, z] in data.iter().array_chunks() {
assert_eq!(x + y + z, 4);
}
Sourcefn array_chunked<const N: usize>(self) -> ArrayChunks<Self, N> ⓘwhere
Self: Sized,
fn array_chunked<const N: usize>(self) -> ArrayChunks<Self, N> ⓘwhere
Self: Sized,
Identical to array_chunks
but doesn’t
collide with the standard library name.