What's a good way to operate on one element in an array while still having access to the rest of the array? For example, below is some sample code that does not compile:
fn example(&mut self, n: usize) {
for i in 0..n {
if Some(x) = &mut self.items[i] {
// some code modifying x
// j = some other index dependent on x
if self.items[j].is_some() { // <--------- cannot borrow self.items[j]
// do something to x
} else {
// do something else to x
}
}
}
}
Here j cannot be determined until x is read, and element j in the array is being read only. Would this be a good candidate to use unsafe (I'm also using tokio so I'm not sure if that would cause unintended consequences)?