Cannot obtain a mutable reference when iterating a recursive structure: cannot borrow as mutable more than once at a time

前端 未结 4 1756
盖世英雄少女心
盖世英雄少女心 2020-11-22 07:05

I\'m trying to navigate a recursive data structure iteratively in order to insert elements at a certain position. To my limited understanding, this means taking a mutable re

相关标签:
4条回答
  • 2020-11-22 07:46

    The original code works as-is once non-lexical lifetimes are enabled:

    type Link = Option<Box<Node>>;
    
    struct Node {
        next: Link,
    }
    
    struct Recursive {
        root: Link,
    }
    
    impl Recursive {
        fn back(&mut self) -> &mut Link {
            let mut anchor = &mut self.root;
            while let Some(node) = anchor {
                anchor = &mut node.next;
            }
            anchor
        }
    }
    

    Non-lexical lifetimes increases the precision of the compiler's borrow checker, allowing it to see that the mutable borrow of anchor is no longer used. We can also simplify the keywords in the if let due to recent language changes.

    0 讨论(0)
  • 2020-11-22 07:46

    You can use recursion to satisfy the borrow checker. This has the disadvantage of creating a stack frame for every item in your list. If your list is long, this will definitely run into a stack overflow. LLVM will optimize the Node::back method into a loop (see the LLVM IR generated on the playground)

    impl Node {
        fn back(&mut self) -> &mut Link {
            match self.next {
                Some(ref mut node) => node.back(),
                None => &mut self.next,
            }
        }
    }
    
    impl Recursive {
        fn back(&mut self) -> Option<&mut Link> {
            self.root.as_mut().map(|node| node.back())
        }
    }
    
    0 讨论(0)
  • 2020-11-22 07:49

    It works:

    fn back(&mut self) -> &mut Link {
        let mut anchor = &mut self.root;
        while anchor.is_some(){
            anchor = &mut {anchor}.as_mut().unwrap().next;
        }
        anchor
    }
    
    0 讨论(0)
  • 2020-11-22 08:05

    It is possible... but I wish I had a more elegant solution.

    The trick is NOT to borrow from anchor, and therefore to juggle between two accumulators:

    • one holding the reference to the current node
    • the other being assigned the reference to the next node

    This leads me to:

    impl Recursive {
        fn back(&mut self) -> &mut Link {
            let mut anchor = &mut self.root;
    
            loop {
                let tmp = anchor;
                if let Some(ref mut node) = *tmp {
                    anchor = &mut node.next;
                } else {
                    anchor = tmp;
                    break;
                }
            }
    
            anchor
        }
    }
    

    Not exactly pretty, but this is something the borrow checker can get behind so ¯\_(ツ)_/¯.

    @ker has improved on this by creating an unnamed temporary:

    impl Recursive {
        fn back(&mut self) -> &mut Link {
            let mut anchor = &mut self.root;
    
            loop {
                match {anchor} {
                    &mut Some(ref mut node) => anchor = &mut node.next,
                    other => return other,
                }
            }
        }
    }
    

    The trick here is that using {anchor} moves the content of anchor into an unnamed temporary on which the match executes. Therefore, in the match block we are not borrowing from anchor but from the temporary, leaving us free to modify anchor. See the related blog post Stuff the Identity Function Does (in Rust).

    0 讨论(0)
提交回复
热议问题