I am porting this algorithm to Rust
The blue sections require exclusive/mutable borrow on the
slice
while the pink requires a shared borrow.
In rust, it is
fn orient(layers: &mut Vec<Layer>) {
for layer_idx in 0..layers.len() - 1 {
for (i, polygon) in layers[layer_idx].iter_mut().enumerate() {
// type checking
let polygon: &mut Polygon = polygon;
if let Some(p) = polygon.first() {
// Internal/External Index
let mut iei = 0;
// loop the layers(excluding the current(this `i`) layer) again
for (_, polygon) in layers[layer_idx]
.iter()
.enumerate()
.filter(|(j, _)| *j != i)
{
if point_in_polygon(*p, polygon) == PointInPolygon::Inside {
iei += 1;
}
}
// dummy condition
if true {
polygon.revert_direction();
}
}
}
}
}
error[E0502]: cannot borrow `*layers` as immutable because it is also borrowed as mutable
--> src/lib.rs:74:37
|
66 | for (i, polygon) in layers[layer_idx].iter_mut().enumerate() {
| ----------------------------------------
| |
| mutable borrow occurs here
| mutable borrow later used here
...
75 | for (_, polygon) in layers[layer_idx]
| ^^^^^^ immutable borrow occurs here
How should I accomplish it?