How to change a value inside an array, iterating over it in Rust

I want to change the value inside the loop, as in the comment. It should be simple, but I do not see a solution.

fn main() { let mut grid: [[i32; 10]; 10] = [[5; 10]; 10]; for (i, row) in grid.iter_mut().enumerate() { for (y, col) in row.iter_mut().enumerate() { //grid[i][y] = 7; print!("{}", col); } print!("{}","\n"); } } 
+5
source share
1 answer

The iter_mut iterator gives you a link to an element that you can use to change the grid. You should usually not use indexes.

 fn main() { let mut grid: [[i32; 10]; 10] = [[5; 10]; 10]; for row in grid.iter_mut() { for cell in row.iter_mut() { *cell = 7; } } println!("{:?}", grid) } 

Playground

+9
source

Source: https://habr.com/ru/post/1275008/


All Articles