# Help for an borrow cehcking error

**URL:** <https://users.rust-lang.org/t/help-for-an-borrow-cehcking-error/140217>\
**Category:** help\
**Created:** [May 22, 2026, 8:43am UTC](https://users.rust-lang.org/t/help-for-an-borrow-cehcking-error/140217 "2026-05-22T08:43:06Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![ojiichan1457-cmd](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/ojiichan1457-cmd/32/50736_2.png) [@ojiichan1457-cmd](https://users.rust-lang.org/u/ojiichan1457-cmd)\
**Post date:** [May 22, 2026, 8:43am UTC](https://users.rust-lang.org/t/help-for-an-borrow-cehcking-error/140217/1 "2026-05-22T08:43:06Z")

</div>

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:

```rust
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?

---

<div class="post-metadata">

**Author:** ![ProgramCrafter](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/programcrafter/32/45916_2.png) [@ProgramCrafter](https://users.rust-lang.org/u/ProgramCrafter)\
**Post date:** [May 22, 2026, 8:57am UTC](https://users.rust-lang.org/t/help-for-an-borrow-cehcking-error/140217/2 "2026-05-22T08:57:25Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![quinedot](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/quinedot/32/17693_2.png) [@quinedot](https://users.rust-lang.org/u/quinedot)\
**Post date:** [May 22, 2026, 9:03am UTC](https://users.rust-lang.org/t/help-for-an-borrow-cehcking-error/140217/3 "2026-05-22T09:03:25Z")

</div>

> [@ojiichan1457-cmd](#):
>
> 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.

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.

---

<div class="post-metadata">

**Author:** ![ojiichan1457-cmd](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/ojiichan1457-cmd/32/50736_2.png) [@ojiichan1457-cmd](https://users.rust-lang.org/u/ojiichan1457-cmd)\
**Post date:** [May 22, 2026, 9:09am UTC](https://users.rust-lang.org/t/help-for-an-borrow-cehcking-error/140217/4 "2026-05-22T09:09:55Z")

</div>

I see!

Now I get it.

I really appreciate you for the prompt responses and advice!

---

<div class="post-metadata">

**Author:** ![system](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/system/32/51372_2.png) [@system](https://users.rust-lang.org/u/system)\
**Post date:** [August 20, 2026, 9:10am UTC](https://users.rust-lang.org/t/help-for-an-borrow-cehcking-error/140217/5 "2026-08-20T09:10:02Z")

</div>

Closed on August 20, 2026, 9:10am UTC

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.
