Ael*_*fyr 5 reference mutable rust borrowing
我试图递归一个节点结构,修改它们,然后返回Node我得到的最后一个。我使用非词法生命周期 RFC 中的示例解决了循环中可变引用的问题。如果我尝试将可变引用返回到 last 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
}
Run Code Online (Sandbox Code Playgroud)
给出这个错误
#[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
}
Run Code Online (Sandbox Code Playgroud)
如果我返回临时值而不是破坏,我会收到错误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,
}
}
}
Run Code Online (Sandbox Code Playgroud)
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
Run Code Online (Sandbox Code Playgroud)
如何使用可变引用遍历结构并返回最后一个Node?我已经搜索过,但我还没有找到针对这个特定问题的任何解决方案。
我不能使用通过迭代递归结构来获取可变引用,因为它给了我多次借用错误:
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
}
Run Code Online (Sandbox Code Playgroud)
这确实与在迭代递归结构时无法获得可变引用不同:一次不能多次借用可变引用。如果我们查看那里的答案,稍作修改,我们可以看到它匹配一个值,并且能够返回在终端情况下匹配的值。也就是说,返回值是一个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
}
}
}
Run Code Online (Sandbox Code Playgroud)
您的情况因两个方面而变得复杂:
您想在一种情况下(有孩子)而不是在另一种情况下(没有孩子)采用可变引用并“放弃”这一事实。这在概念上与此相同:
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),
}
}
Run Code Online (Sandbox Code Playgroud)
编译器无法判断该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,
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
请注意,我们正在使用从 HashMap 或 Vec 返回引用中公开的所有技术会导致借用持续超出其所在的范围?并且在迭代递归结构时无法获得可变引用:一次不能多次借用可变引用。
通过正在进行的 NLL 重新实现,我们可以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),
}
}
Run Code Online (Sandbox Code Playgroud)
Polonius 的最终版本应该允许将整个问题简化为一个非常简单的形式:
fn get_last(mut current: &mut Node) -> &mut Node {
while let Some(child) = current.get_last() {
current = child;
}
current
}
Run Code Online (Sandbox Code Playgroud)
也可以看看: