std-method

slice::chunks_exact

Iterate only over full-size groups. Use remainder() to inspect the trailing elements that do not form a full group. Zero is invalid. Expected stdout (not executed): [1, 2] [3, 4] left: [5] Common mistake: Forgetting to check the remainder when parsing fixed-width records.

Formal Syntax & Structure

rust syntaxrust
slice.chunks_exact(chunk_size)

Working Example

Example Snippetrust
fn main() {    let bytes = [1, 2, 3, 4, 5];    let mut pairs = bytes.chunks_exact(2);    for pair in &mut pairs { println!("{pair:?}"); }    println!("left: {:?}", pairs.remainder());}
Rust Reference