How to print array elements in groups of 2 with Rust

1 Answer

0 votes
fn main() {
    let arr = [1, 2, 3, 4, 5, 6, 7];

    // Iterate over the array in chunks of 2
    for chunk in arr.chunks(2) {
        println!("{:?}", chunk);
    }
}

   
    
/*
run:
    
[1, 2]
[3, 4]
[5, 6]
[7]
    
*/

 



answered Jun 21, 2025 by avibootz
...