gpt4 book ai didi

reference - 使用可变引用迭代递归结构并返回最后一个有效引用

转载 作者:行者123 更新时间:2023-11-29 08:06:09 25 4
gpt4 key购买 nike

我正在尝试向下递归节点结构,修改它们,然后返回我到达的最后一个 Node。我使用 example in the non-lexical lifetimes RFC 解决了循环中可变引用的问题.如果我尝试返回对最后一个 Node 的可变引用,我会得到一个 use of moved value 错误:

#[derive(Debug)]
struct Node {
children: Vec<Node>,
}

impl Node {
fn new(children: Vec<Self>) -> Self {
Self { children }
}
fn get_last(&mut self) -> Option<&mut Node> {
self.children.last_mut()
}
}

fn main() {
let mut root = Node::new(vec![Node::new(vec![])]);

let current = &mut root;

println!("Final: {:?}", get_last(current));
}


fn get_last(mut current: &mut Node) -> &mut Node {
loop {
let temp = current;
println!("{:?}", temp);

match temp.get_last() {
Some(child) => { current = child },
None => break,
}
}

current
}

给出这个错误

error[E0382]: use of moved value: `*current`
--> test.rs:51:5
|
40 | let temp = current;
| ---- value moved here
...
51 | current
| ^^^^^^^ value used here after move
|
= note: move occurs because `current` has type `&mut Node`, which does not implement the `Copy` trait

如果我返回临时值而不是中断,我会得到错误cannot borrow as mutable more than once

fn get_last(mut current: &mut Node) -> &mut Node {
loop {
let temp = current;
println!("{:?}", temp);

match temp.get_last() {
Some(child) => { current = child },
None => return temp,
}
}
}
error[E0499]: cannot borrow `*temp` as mutable more than once at a time
--> test.rs:47:28
|
43 | match temp.get_last() {
| ---- first mutable borrow occurs here
...
47 | None => return temp,
| ^^^^ second mutable borrow occurs here
48 | }
49 | }
| - first borrow ends here

如何使用可变引用遍历结构并返回最后一个 Node?我进行了搜索,但没有找到针对此特定问题的任何解决方案。

我不能使用 Obtaining a mutable reference by iterating a recursive structure因为它给我一个借用不止一次的错误:

fn get_last(mut current: &mut Node) -> &mut Node {
loop {
let temp = current;
println!("{:?}", temp);

match temp.get_last() {
Some(child) => current = child,
None => current = temp,
}
}
current
}

最佳答案

这确实不同于Cannot obtain a mutable reference when iterating a recursive structure: cannot borrow as mutable more than once at a time .如果我们查看那里的答案,稍微修改一下,我们可以看到它匹配一个值,并且能够返回在终端案例中匹配的值。也就是说,返回值是一个Option:

fn back(&mut self) -> &mut Option<Box<Node>> {
let mut anchor = &mut self.root;

loop {
match {anchor} {
&mut Some(ref mut node) => anchor = &mut node.next,
other => return other, // transferred ownership to here
}
}
}

您的情况在两个方面很复杂:

  1. 缺少non-lexical lifetimes .
  2. 在一种情况下(有 child )而不是在另一种情况下(没有 child )您想要获取可变引用并“放弃”这一事实。这在概念上与此相同:

    fn maybe_identity<T>(_: T) -> Option<T> { None }

    fn main() {
    let name = String::from("vivian");

    match maybe_identity(name) {
    Some(x) => println!("{}", x),
    None => println!("{}", name),
    }
    }

    编译器无法判断 None 情况可以(非常理论上)继续使用 name

直接的解决方案是明确编码这个“找回” Action 。我们创建一个枚举,在没有 child 的情况下返回 &mut self,一个返回该枚举的辅助方法,并重写主要方法以使用辅助方法:

enum LastOrNot<'a> {
Last(&'a mut Node),
NotLast(&'a mut Node),
}

impl Node {
fn get_last_or_self(&mut self) -> LastOrNot<'_> {
match self.children.is_empty() {
false => LastOrNot::Last(self.children.last_mut().unwrap()),
true => LastOrNot::NotLast(self),
}
}

fn get_last(mut current: &mut Node) -> &mut Node {
loop {
match { current }.get_last_or_self() {
LastOrNot::Last(child) => current = child,
LastOrNot::NotLast(end) => return end,
}
}
}
}

请注意,我们正在使用 Returning a reference from a HashMap or Vec causes a borrow to last beyond the scope it's in? 中公开的所有技术和 Cannot obtain a mutable reference when iterating a recursive structure: cannot borrow as mutable more than once at a time .


正在进行中 reimplementation of NLL ,我们可以稍微简化 get_last_or_self 以避免 bool 值:

fn get_last_or_self(&mut self) -> LastOrNot<'_> {
match self.children.last_mut() {
Some(l) => LastOrNot::Last(l),
None => LastOrNot::NotLast(self),
}
}

Polonius 的最终版本应该允许将整个问题简化为非常的简单形式:

fn get_last(mut current: &mut Node) -> &mut Node {
while let Some(child) = current.get_last() {
current = child;
}

current
}

另见:

关于reference - 使用可变引用迭代递归结构并返回最后一个有效引用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48610593/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com