gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 11:31:36 24 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
}
}
}

您的案例有两个方面很复杂:
  • 缺少non-lexical lifetimes
  • 您希望在一个情况下(有 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

  • 直接的解决方案是显式地编码此“取回”操作。我们创建一个枚举,该枚举在没有子代的情况下返回 &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
    }

    也可以看看:
  • 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
  • 关于reference - 使用可变引用遍历递归结构并返回最后一个有效引用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61897392/

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