使用可变引用遍历递归结构并返回最后一个有效引用 [英] Iterating through a recursive structure using mutable references and returning the last valid reference

查看:85
本文介绍了使用可变引用遍历递归结构并返回最后一个有效引用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试递归节点的结构,对其进行修改,然后返回我所得到的最后一个Node.我使用

给出此错误

 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?我进行了搜索,但没有找到针对此特定问题的任何解决方案.

我不能使用通过迭代获取可变引用递归结构,因为它给了我多次借款的错误:

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
}

解决方案

这确实与迭代时无法获取可变引用递归结构:一次不能多次借用可变变量.如果我们看一下那里的答案,稍作修改,就可以看到它与一个值匹配,并且能够返回在终端情况下匹配的值.也就是说,返回值是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. 缺少非词法生存期.
  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情况可以(理论上是非常)继续使用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的所有借用都超出了它的适用范围?迭代递归结构时无法获得可变引用:一次不能多次借用可变变量.. >

在使用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),
    }
}

通过NLL的重新实现,整个问题可以简化为非常的简单形式:

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

    current
}

另请参阅:

I'm trying to recurse down a structure of nodes, modifying them, and then returning the last Node that I get to. I solved the problems with mutable references in the loop using an example in the non-lexical lifetimes RFC. If I try to return the mutable reference to the last Node, I get a 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
}

Gives this error

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

If I return the temporary value instead of breaking, I get the error 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

How can I iterate through the structure with mutable references and return the last Node? I've searched, but I haven't found any solutions for this specific problem.

I can't use Obtaining a mutable reference by iterating a recursive structure because it gives me a borrowing more than once error:

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
}

解决方案

This is indeed different from Cannot obtain a mutable reference when iterating a recursive structure: cannot borrow as mutable more than once at a time. If we look at the answer there, modified a bit, we can see that it matches on a value and is able to return the value that was matched on in the terminal case. That is, the return value is an 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
        }
    }
}

Your case is complicated by two aspects:

  1. The lack of non-lexical lifetimes.
  2. The fact that you want to take a mutable reference and "give it up" in one case (there are children) and not in the other (no children). This is conceptually the same as this:

    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),
        }
    }
    

    The compiler cannot tell that the None case could (very theoretically) continue to use name.

The straight-forward solution is to encode this "get it back" action explicitly. We create an enum that returns the &mut self in the case of no children:

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),
        }
    }
}

The function can then be rewritten to use the enum:

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,
        }
    }
}

Note that we are using all of the techniques exposed in both Rust borrow of a HashMap lasts beyond the scope it's in? and Cannot obtain a mutable reference when iterating a recursive structure: cannot borrow as mutable more than once at a time.

With NLL available, we can simplify get_last_or_self a bit to avoid the boolean:

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

With an in-progress reimplementation of NLL, the entire problem can be reduced to a very simple form:

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

    current
}

See also:

这篇关于使用可变引用遍历递归结构并返回最后一个有效引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆