Help for an borrow cehcking error

I encountered an error associated with borrow checking that seems not to follow the nonlexical lifetime analysis, so I would like you to help me figure out what the real cause is.

Here is my implementation:

impl Minimize for Vec<[Int; 2]> {
    fn minimize(&mut self) {
        let mut len = self.len();
        let mut prior_idx = 0_usize;
        let mut subseq_idx = 1_usize;

        while prior_idx <= len - 2 {
            let prior_node = &self[prior_idx]; // Cause 1.
            while subseq_idx <= len - 1 {
                let subseq_node = &self[subseq_idx]; // Cause 2
                if prior_node.equals(subseq_node) {
                    self.remove(subseq_idx); // The error occurs here.
                    len -= 1;
                } else {
                    subseq_idx += 1;
                }
            }

            prior_idx += 1;
        }
    }
}

According to the compiler diagnostic, the error seems to have been triggered because I tried borrowing &mut self while having the two &self's. However, as far as I understand, prior_node and subseq_node should end their lifetimes before reaching the line where the remove method is used based on the NLL rule because I never use them after using the equals method on them.

Is the cause of this error that the lifetimes of those nodes somehow got extended up to the very end of the scope? If so, why does this happen?

At the very least, you do use prior_node after the removal - namely on the next iteration of while.

Also, note that remove from the middle of the vector is slow. You might want to go with two-pointers approach, swapping, and afterwards truncating the vector.

You use prior_node after looping (imagine the inner while loop unrolled). If you move prior_node inside the inner loop, the error is resolved.

I see!

Now I get it.

I really appreciate you for the prompt responses and advice!