rust - 迭代递归结构时无法获取可变引用 : cannot borrow as mutable more than once at a time

标签 rust mutable borrowing

我正在尝试迭代地导航递归数据结构,以便在特定位置插入元素。就我有限的理解而言,这意味着对结构的根进行可变引用,然后用对其跟随者的引用依次替换它:

type Link = Option<Box<Node>>;

struct Node {
    next: Link
}

struct Recursive {
    root: Link
}

impl Recursive {
    fn back(&mut self) -> &mut Link {
        let mut anchor = &mut self.root;
        while let Some(ref mut node) = *anchor {
            anchor = &mut node.next;
        }
        anchor
    }
}

(Rust playground link)

然而,这失败了:

error[E0499]: cannot borrow `anchor.0` as mutable more than once at a time
  --> src/main.rs:14:24
   |
14 |         while let Some(ref mut node) = *anchor {
   |                        ^^^^^^^^^^^^
   |                        |
   |                        second mutable borrow occurs here
   |                        first mutable borrow occurs here
...
18 |     }
   |     - first borrow ends here

error[E0506]: cannot assign to `anchor` because it is borrowed
  --> src/main.rs:15:13
   |
14 |         while let Some(ref mut node) = *anchor {
   |                        ------------ borrow of `anchor` occurs here
15 |             anchor = &mut node.next;
   |             ^^^^^^^^^^^^^^^^^^^^^^^ assignment to borrowed `anchor` occurs here

error[E0499]: cannot borrow `*anchor` as mutable more than once at a time
  --> src/main.rs:17:9
   |
14 |         while let Some(ref mut node) = *anchor {
   |                        ------------ first mutable borrow occurs here
...
17 |         anchor
   |         ^^^^^^ second mutable borrow occurs here
18 |     }
   |     - first borrow ends here

这是有道理的,因为 anchornode 都引用相同的结构,但实际上我不再关心 anchor 之后解构它。

back() 如何使用安全的 Rust 正确实现?

最佳答案

这是可能的......但我希望我有一个更优雅的解决方案。

诀窍不是从 anchor 借用,因此要在两个累加器之间兼顾:

  • 一个持有对当前节点的引用
  • 另一个被分配到下一个节点的引用

这导致我:

impl Recursive {
    fn back(&mut self) -> &mut Link {
        let mut anchor = &mut self.root;

        loop {
            let tmp = anchor;
            if let Some(ref mut node) = *tmp {
                anchor = &mut node.next;
            } else {
                anchor = tmp;
                break;
            }
        }

        anchor
    }
}

不是很漂亮,但这是借用检查器可以解决的问题,所以¯\_(ツ)_/¯。

@ker 通过创建一个未命名的临时文件对此进行了改进:

impl Recursive {
    fn back(&mut self) -> &mut Link {
        let mut anchor = &mut self.root;

        loop {
            match {anchor} {
                &mut Some(ref mut node) => anchor = &mut node.next,
                other => return other,
            }
        }
    }
}

这里的技巧是使用 {anchor} 移动 anchor 的内容到匹配执行的未命名的临时文件中。因此,在 match block 中,我们不是从 anchor 借用,而是从临时借用,让我们可以自由修改 anchor。请参阅相关博文 Stuff the Identity Function Does (in Rust) .

关于rust - 迭代递归结构时无法获取可变引用 : cannot borrow as mutable more than once at a time,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40920575/

相关文章:

c++ - 是否实现定义了哪些算法可以接受可变的 lambda?

rust - 从 Rust 中的元组内的自有盒子中借用

rust - Rust-从方法中获取值(value)而无需借用(计算文件行数)

rust - 函数返回不可变引用,但借用检查器不这么认为

string - 有没有办法在将迭代器映射到更大的切片内取消引用切片?

rust - 有没有办法使用 Option::and_then 来缩短非复制类型的匹配表达式?

c# - 如何在 F# 中编写此 C# 代码

javascript - 哪些 JavaScript 数组函数正在发生变化?

rust - 重复更新和输出数据结构

rust - 当两者属于同一类型时匹配一对枚举