不能在循环中多次借用可变错误
Cannot borrow as mutable more than once error in a loop
我正在研究 leetcode 问题 #83 "Remove Duplicates from Sorted List",但我被这个借用检查器问题困住了。
问题给出了 ListNode 结构,因此无法更改。我已经尝试重构循环和 if 语句,但我还没有找到有效的解决方案。
我想做什么:
// Definition for singly-linked list.
#[derive(PartialEq, Eq, Debug)]
pub struct ListNode {
pub val: i32,
pub next: Option<Box<ListNode>>,
}
impl ListNode {
#[inline]
fn new(val: i32) -> Self {
ListNode { next: None, val }
}
}
fn remove_duplicates(mut list: Option<Box<ListNode>>) -> Option<Box<ListNode>> {
let mut cursor = &mut list;
while let Some(c) = cursor.as_mut() {
if let Some(next) = c.next.as_mut() {
if next.val == c.val {
c.next = next.next.take();
continue;
}
}
cursor = &mut c.next;
}
list
}
我遇到的错误:
error[E0499]: cannot borrow `*cursor` as mutable more than once at a time
--> src/lib.rs:17:25
|
17 | while let Some(c) = cursor.as_mut() {
| ^^^^^^ mutable borrow starts here in previous iteration of loop
似乎显示相同错误的简化代码:
fn remove_duplicates(mut list: Option<Box<ListNode>>) -> Option<Box<ListNode>> {
let mut cursor = &mut list;
while let Some(c) = cursor.as_mut() {
if c.val > 0 {
cursor = &mut c.next;
}
}
list
}
我不明白为什么在循环的下一次迭代之前没有删除可变借用。这似乎是由有条件地更改光标引起的,但我不明白为什么这会阻止借用被删除。
这是我最终得到的解决方案。在 if 语句中重新分配 cursor
可以解决问题。
fn remove_duplicates(mut list: Option<Box<ListNode>>) -> Option<Box<ListNode>> {
let mut cursor = list.as_mut();
while let Some(c) = cursor {
if let Some(next) = c.next.as_mut() {
if next.val == c.val {
c.next = next.next.take();
cursor = Some(c);
continue;
}
}
cursor = c.next.as_mut();
}
list
}
我正在研究 leetcode 问题 #83 "Remove Duplicates from Sorted List",但我被这个借用检查器问题困住了。
问题给出了 ListNode 结构,因此无法更改。我已经尝试重构循环和 if 语句,但我还没有找到有效的解决方案。
我想做什么:
// Definition for singly-linked list.
#[derive(PartialEq, Eq, Debug)]
pub struct ListNode {
pub val: i32,
pub next: Option<Box<ListNode>>,
}
impl ListNode {
#[inline]
fn new(val: i32) -> Self {
ListNode { next: None, val }
}
}
fn remove_duplicates(mut list: Option<Box<ListNode>>) -> Option<Box<ListNode>> {
let mut cursor = &mut list;
while let Some(c) = cursor.as_mut() {
if let Some(next) = c.next.as_mut() {
if next.val == c.val {
c.next = next.next.take();
continue;
}
}
cursor = &mut c.next;
}
list
}
我遇到的错误:
error[E0499]: cannot borrow `*cursor` as mutable more than once at a time
--> src/lib.rs:17:25
|
17 | while let Some(c) = cursor.as_mut() {
| ^^^^^^ mutable borrow starts here in previous iteration of loop
似乎显示相同错误的简化代码:
fn remove_duplicates(mut list: Option<Box<ListNode>>) -> Option<Box<ListNode>> {
let mut cursor = &mut list;
while let Some(c) = cursor.as_mut() {
if c.val > 0 {
cursor = &mut c.next;
}
}
list
}
我不明白为什么在循环的下一次迭代之前没有删除可变借用。这似乎是由有条件地更改光标引起的,但我不明白为什么这会阻止借用被删除。
这是我最终得到的解决方案。在 if 语句中重新分配 cursor
可以解决问题。
fn remove_duplicates(mut list: Option<Box<ListNode>>) -> Option<Box<ListNode>> {
let mut cursor = list.as_mut();
while let Some(c) = cursor {
if let Some(next) = c.next.as_mut() {
if next.val == c.val {
c.next = next.next.take();
cursor = Some(c);
continue;
}
}
cursor = c.next.as_mut();
}
list
}