I have this code:
struct Physical;
fn collision(a: &mut Physical, b: &mut Physical) {
//accelerate a and b depending on their values
}
fn main_loop(vec: &mut Vec<Physical>) {
for i in 0..vec.len() {
for j in i+1..vec.len() {
collision(&mut vec[i], &mut vec[j]);
}
}
}
fn main() {
let mut vec = vec![Physical, Physical, Physical];
loop {
main_loop(&mut vec);
}
}
This doesn't compile, because the mutable references are both in the same vector.
It would be possible to change collision
to take indices and the mutable vector, but it looks ugly. I also want to change the Container
type later to make the collision more efficient. I may even want to use multiple containers for my objects, so this won't be an option.
How can this be done the best way?