Help for an borrow cehcking error

⚓ Rust    📅 2026-05-22    👤 surdeus    👁️ 2      

surdeus

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?

4 posts - 3 participants

Read full topic

🏷️ Rust_feed