Trait const_chunks::IteratorConstChunks
source · pub trait IteratorConstChunks {
type Inner: Iterator;
// Required method
fn const_chunks<const N: usize>(self) -> ConstChunks<N, Self::Inner> ⓘ;
}Expand description
An extension trait providing Iterators with the capability to iterate
over chunks of items.
Required Associated Types§
Required Methods§
sourcefn const_chunks<const N: usize>(self) -> ConstChunks<N, Self::Inner> ⓘ
fn const_chunks<const N: usize>(self) -> ConstChunks<N, Self::Inner> ⓘ
This function returns an iterator over constant-length chunks of items, where the length is provided as a const-generic. This function assumes that the number of items can be divided into an integer number of chunks. If there are not enough items to fully fill a chunk, then the items are consumed, but no chunk will be yielded.
Examples
Basic usage:
use const_chunks::IteratorConstChunks;
let v = vec![1, 2, 3, 4, 5, 6];
let mut v_iter = v.into_iter().const_chunks::<2>();
assert_eq!(v_iter.next(), Some([1,2]));
assert_eq!(v_iter.next(), Some([3,4]));
assert_eq!(v_iter.next(), Some([5,6]));When the number of items in the iterator cannot be divided exactly into chunks, then the iterator will be fully consumed, but the last chunk will not be yielded.
use const_chunks::IteratorConstChunks;
let v = (1..=5).map(|n| n.to_string()).collect::<Vec<String>>(); // Five items cannot fit into chunks of length 2!
let mut v_iter = v.into_iter().const_chunks::<2>();
assert_eq!(v_iter.next(), Some([String::from("1"),String::from("2")]));
assert_eq!(v_iter.next(), Some([String::from("3"),String::from("4")]));
assert_eq!(v_iter.next(), None); // `None`, even though there was still one item