问题
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
}
    回答1:
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:
- The lack of non-lexical lifetimes.
 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
Nonecase could (very theoretically) continue to usename.
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:
- Rust borrow of a HashMap lasts 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
 
来源:https://stackoverflow.com/questions/48610593/iterating-through-a-recursive-structure-using-mutable-references-and-returning-t