Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
988 views
in Technique[技术] by (71.8m points)

rust - Iterating through a recursive structure using mutable references and returning the last valid reference

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
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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, a helper method that returns that enum, and rewrite the primary method to use the helper:

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

Note that we are using all of the techniques exposed in both Returning a reference from a HashMap or Vec causes a borrow to last 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 an in-progress reimplementation of NLL, 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),
    }
}

The final version of Polonius should allow reducing the entire problem 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:


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...