我想實現我的二叉樹的core :: fmt :: Show。這是我的實現代碼:試圖執行core :: fmt ::顯示
impl<T: PartialEq + PartialOrd + Show> Show for Node<T>
{
fn fmt(&self, f: &mut Formatter) -> Result<(), &str>
{
match self.left {
Some(ref x) => {x.fmt(f);},
None => {}
};
match self.value {
Some(ref x) => {
write!(f, "{}", x.to_string().as_slice());
},
None => {}
};
match self.right {
Some(ref x) => {x.fmt(f);},
None => {}
};
Ok(())
}
}
但是,編譯器會引發以下錯誤:
Compiling binary_tree v0.0.1 (file:///home/guillaume/projects/binary_tree) src/binary_tree.rs:60:2: 77:3 error: method
fmt
has an incompatible type for trait: expected enum core::fmt::FormatError, found &-ptr [E0053] src/binary_tree.rs:60 fn fmt(&self, f: &mut Formatter) -> Result<(), &str> src/binary_tree.rs:61 { src/binary_tree.rs:62 match self.left { src/binary_tree.rs:63 Some(ref x) => {x.fmt(f);}, src/binary_tree.rs:64 None => {} src/binary_tree.rs:65 };
我不明白爲什麼。完整的代碼可以找到here。有關我的代碼的任何意見,歡迎。
最好使用'fmt :: Result'而不是顯式的'Result <(),fmt :: FormatError>'。這就是Show特質本身的作用。 –
非常感謝您花時間更正我的代碼! – Moebius