pub trait IterChunks: Sized + Iterator {
// Required method
fn chunks(self, n: usize) -> Chunks<Self>;
}
Expand description
A trait that extends Iterator
with chunks
method.
Required Methods§
Sourcefn chunks(self, n: usize) -> Chunks<Self>
fn chunks(self, n: usize) -> Chunks<Self>
Create an iterator-liked struct that yields elements by chunk every n elements, or fewer if the underlying iterator ends sooner.
Chunks
is not a real Iterator, but a LendingIterator, which is
currently not in std and blocked by GAT. We have to iterate with
while loop now.
use iter_chunks::IterChunks;
let arr = [1, 1, 2, 2, 3];
let expected = [vec![1, 1], vec![2, 2], vec![3]];
let mut chunks = arr.into_iter().chunks(2);
let mut i = 0;
while let Some(chunk) = chunks.next() {
assert_eq!(chunk.collect::<Vec<_>>(), expected[i]);
i += 1;
}
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.