Borrowing a slice exclusivly and then borrowing it again

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?

I think the use of this helper function does what you want.

/// Returns the exclusive slice before `idx`, an exclusive reference to the
/// element at `idx`, and the exclusive slice after `idx`.
///
/// # Panics
///
/// Panics if `idx` is out of bounds.
fn dbl_split<T>(slice: &mut [T], idx: usize) -> (&mut [T], &mut T, &mut [T]) {
    let (head, rest) = slice.split_at_mut(idx);
    let (middle, tail) = rest.split_first_mut().unwrap();
    (head, middle, tail)
}
        let polygons = &mut layers[layer_idx][..];
        for i in 0..polygons.len() {
            let (head, polygon, tail) = dbl_split(polygons, i);
            if let Some(p) = polygon.first() {
                // Internal/External Index
                let mut iei = 0;

                // loop the layers (excluding the current layer) again
                for polygon in head.into_iter().chain(tail) {