I’m writing a video game and as part of world generation, I need to iterate over each tile and check if there is a water tile anywhere in the vicinity. Whether there is or isn’t, I’ll still be mutating the items from the outer loop, but the inner loop is immutable. Important note: I’m excluding the current item from the outer loop in my coverage of the inner loop. In this case, splitting out the two arrays is a non-option for me. My code looks something like this (extremely simplified):
for (y, row) in world_map.iter_mut().enumerate() {
for (x, unit) in row.iter_mut().enumerate() {
// Do other testing stuff concerning unit
let mut water_close = false;
for wx in (-5)..5 {
for wy in (-5)..5 {
if wx != 0 && wy != 0 {
let tile = &world_map[(wy + (y as i32)) as usize]
[(wx + (x as i32)) as usize];
if matches!(tile, Tile::Water(..)) {
water_close = true;
break;
}
}
}
}
}
// Modify unit based on water_close.
}
Any suggestions? Thanks!