I am writing a tree in Rust and I want to implement a function:left which can turn a tree's root into the tree's root's left child.
Here is my code:
struct Node {
value: i32,
left: Option<Box<Node>>,
right: Option<Box<Node>>,
}
struct Tree {
root: Option<Box<Node>>,
}
enum Error {
DuplicateValue,
NotFound,
EmptyTree
}
impl Tree {
fn left(&mut self) -> Result<(), Error> {
match self.root {
Option::None => return Err(Error::EmptyTree),
Option::Some(ref mut node) => {
*node = node.left.as_ref().unwrap();
return Ok(())
}
}
}
}
and the error says
mismatched types
expected struct `Box<Node>`
found reference `&Box<Node>`
I have tried many methods and look up a lot of information but I still cannot fix this. I think it is a very simple thing in C, just like:
*root = *(root->left);
but why is it so hard in Rust? Can anyone help me?