I want to change the value inside the loop like in the comment. It should be simple but I don't see the 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");
}
}
The iter_mut
iterator gives you a reference to the element, which you can use to mutate the grid. You usually shouldn't use indices.
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 link
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With