Function as_chunks

Source
pub const fn as_chunks<T, const N: usize>(vals: &[T]) -> (&[[T; N]], &[T])
Expand description

Splits the slice into a slice of N-element arrays, starting at the beginning of the slice, and a remainder slice with length strictly less than N.

§Panics

Panics if N is 0. This check will most probably get changed to a compile time error before this method gets stabilized.

§Examples

let slice = ['l', 'o', 'r', 'e', 'm'];
let (chunks, remainder) = array_util::as_chunks(&slice);
assert_eq!(chunks, &[['l', 'o'], ['r', 'e']]);
assert_eq!(remainder, &['m']);

If you expect the slice to be an exact multiple, you can combine let-else with an empty slice pattern:

let slice = ['R', 'u', 's', 't'];
let (chunks, []) = array_util::as_chunks::<_, 2>(&slice) else {
    panic!("slice didn't have even length")
};
assert_eq!(chunks, &[['R', 'u'], ['s', 't']]);