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

﹥>﹥吖頭↗ 提交于 2019-12-01 23:02:58

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:

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!