I'm trying to implement a binary search tree. The parent struct, BinSearchTree, has a pointer to a root node:
pub struct BinSearchTree { root: Option<Box<Node>>, size: u32, } Each node holds a value (currently u32 are supported; I'll make it generic later) and two pointers to its branches:
struct Node { value: u32, left: Option<Box<Node>>, right: Option<Box<Node>>, } Because dropping a value can cause the tree to change, my drop_value method, called on a Node, takes the Node as a moved value and returns a new Box pointing to itself, or to one of its children, which the parent Node uses to replace the moved value. Within Node, the method has this signature and it works:
pub fn drop_value(mut self, value: u32) -> (Result<(),TreeError>, Option<Box<Node>>) { Note that it consumes itself (mut self).
My problem is at the root struct. BinSearchTree::drop_value isn't supposed to return a new BinSearchTree, just replace the field root. So the signature uses &mut self:
pub fn drop_value(&mut self, value: u32) -> Result<(),TreeError> { match self.root { None => { self.root = None; // not my real code return Err(TreeError::ValueNotFound); }, Some(child) => { self.root = Some(child); // not my real code return Ok(()); }, } } Here's what I don't understand: I get an error on the line match self.root { that says "cannot move out of self.root as enum variant Some which is behind a mutable reference". For some reason, in the Some branch of the match, I can't replace the root with a new value. (Note that I've shortened the code; in the real program I call drop_value on the child and use the result. Nevertheless I get the same compile error with this shortened version.) It works if I change the signature to use mut self but I don't want to do that. Why can't I replace the root?