I know it's a redundant question with rust but it seems I can not manage to get some generality from it. So I attempted to write a simple binary tree using some code from elsewhere (at first I used a Box of options, but it seems options of box are the way to go).
#[derive(Clone)]
pub struct BinaryTree<T>
where T:Clone
{
pub val: T,
pub left : Option<Box<BinaryTree<T>>>,
pub right : Option<Box<BinaryTree<T>>>,
}
impl<T: std::clone::Clone> BinaryTree<T>
{
pub fn new(val:T) -> Self
{
BinaryTree
{
val,
left:None,
right:None,
}
}
pub fn insertleft( mut self, node: BinaryTree<T>)-> Self
{
//let old_left = mem::take(&mut self.left);
let left_option = Some(Box::new(node ));
self.left = left_option;
self
}
pub fn insertright( mut self, node: BinaryTree<T>)-> Self
{
let right_option = Some(Box::new( node ));
self.right = right_option;
self
}
}
So here is my problem. If I create a tree like that:
let tree = BinaryTree::new(1).insertleft(BinaryTree::new(2)).insertright(BinaryTree::new(3));
There is actually no problem and I can access the left value, the right value without any problem. But if I try to fill the tree step by step
let tree = BinaryTree::new(1);
tree.insertleft(BinaryTree::new(2));
tree.insertright(BinaryTree::new(3));
then I run into errors like "use of moved value: tree.left" whether I declare tree mutable or not. I do not really get where or how the tree is supposed to be "consumed" here. If I use & mut ref instead of ref in the insertleft and insertright functions I run into similar errors such as can not move out of which is behind a mutable reference.
I read plenty of topics about such problems but I really do not get why you can not access a public field by what is in the end, more or less a setter.