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

2024-01-13

我正在尝试递归节点结构,修改它们,然后返回最后一个Node我能做到的。我使用循环中的可变引用解决了问题非词汇生命周期 RFC 中的示例 https://github.com/rust-lang/rfcs/blob/master/text/2094-nll.md#problem-case-4-mutating-mut-references。如果我尝试将可变引用返回到最后一个Node,我得到一个use of moved value error:

#[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?我已经搜索过,但没有找到针对这个特定问题的任何解决方案。

我不能使用通过迭代递归结构获取可变引用 https://stackoverflow.com/questions/37986640/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
}

这确实不同于迭代递归结构时无法获取可变引用:一次不能多次借用可变引用 https://stackoverflow.com/q/37986640/155423。如果我们查看那里的答案,稍微修改一下,我们可以看到它匹配一个值,并且能够返回在终端情况下匹配的值。也就是说,返回值是一个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. 缺乏非词汇生命周期 https://stackoverflow.com/q/50251487/155423.
  2. 事实上,您希望在一种情况下(有子项)采用可变引用并“放弃”,而在另一种情况下(没有子项)则“放弃”。这在概念上与此相同:

    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情况可以(very理论上)继续使用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,
            }
        }
    }
}

请注意,我们正在使用两者中公开的所有技术从 HashMap 或 Vec 返回引用会导致借用超出其范围? https://stackoverflow.com/q/38023871/155423 and 迭代递归结构时无法获取可变引用:一次不能多次借用可变引用 https://stackoverflow.com/q/37986640/155423.


正在进行中重新实施NLL https://github.com/rust-lang/polonius,我们可以简化get_last_or_self有点避免布尔值:

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

Polonius 的最终版本应该可以将整个问题简化为very简单的形式:

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

    current
}

也可以看看:

  • 从 HashMap 或 Vec 返回引用会导致借用超出其范围? https://stackoverflow.com/q/38023871/155423
  • 迭代递归结构时无法获取可变引用:一次不能多次借用可变引用 https://stackoverflow.com/q/37986640/155423
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

使用可变引用迭代递归结构并返回最后一个有效引用 的相关文章

随机推荐