I'm trying to write a program to evenly (or as close to possible) distribute points about the surface of a sphere. I'm trying to achieve this by placing N points randomly about a unit sphere, and then running multiple steps where the points repel each other.
The problem is in the loop over the points array. The code below loops over each point, and then the loop inside that, again loops over each point and calculates the repulsive force between each point pair.
for point in points.iter_mut() {
point.movement = Quaternion::identity();
for neighbour in &points {
if neighbour.id == point.id {
continue;
}
let angle = point.pos.angle(&neighbour.pos);
let axis = point.pos.cross(&neighbour.pos);
let force = -(1.0/(angle*angle)) * update_amt;
point.movement = point.movement * Quaternion::angle_axis(angle, axis);
}
}
I'm getting the error:
src/main.rs:71:27: 71:33 error: cannot borrow `points` as immutable because it is also borrowed as mutable
src/main.rs:71 for neighbour in &points {
and the explanation
the mutable borrow prevents subsequent moves, borrows, or modification of `points` until the borrow ends
I'm fairly new to Rust, coming from a C++ background, and I've got no idea how to make this kind of pattern work in Rust.
Any solutions to this would be greatly appreciated, as I'm now absolutely stuck for ideas.