I'm trying to write a recursive method that adds an item to a tree and returns the tree node corresponding to that item.
enum BstNode {
Node(int, ~BstNode, ~BstNode),
Leaf
}
impl BstNode {
fn insert<'a>(&'a mut self, item: int) -> &'a mut BstNode {
match *self {
Leaf => {
*self = Node(item, ~Leaf, ~Leaf);
self
},
Node(ref node_item, ref mut left, ref mut right) =>
match item.cmp(node_item) {
Less => left.insert(item),
Equal => self,
Greater => right.insert(item)
}
}
}
}
I'm bitten by the following error:
bst.rs:19:30: 19:34 error: cannot move out of `self` because it is borrowed
bst.rs:19 Equal => self,
^~~~
bst.rs:16:18: 16:31 note: borrow of `self#0` occurs here
bst.rs:16 Node(ref node_item, ref mut left, ref mut right) =>
^~~~~~~~~~~~~
What does "moving out of something
" mean? How do I fix this error?
I'm using Rust 0.10.