我正在用Rust编写一棵树,我想实现一个函数:left
,它可以将树的根转换为树的根的左子对象.
以下是我的代码:
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(())
}
}
}
}
错误是说
mismatched types
expected struct `Box<Node>`
found reference `&Box<Node>`
我try 了很多方法,查阅了很多信息,但我仍然无法修复这个问题.我认为这在C中是非常简单的事情,就像:
*root = *(root->left);
但为什么在铁 rust 地区这么难呢?有谁可以帮我?