std-method

slice::chunks

Walk through nonoverlapping groups. The final group can be shorter than the requested size. The chunk size must be positive. Expected stdout (not executed): [2, 4] [6, 8] [10] Common mistake: Assuming all groups have exactly the same length.

Formal Syntax & Structure

rust syntaxrust
slice.chunks(chunk_size)

Working Example

Example Snippetrust
fn main() {    let readings = [2, 4, 6, 8, 10];    for group in readings.chunks(2) {        println!("{group:?}");    }}
Rust Reference