【发布时间】:2018-01-04 12:40:30
【问题描述】:
我有这个结构:
pub struct Node<T> {
value: T,
left: Option<Box<Node<T>>>,
right: Option<Box<Node<T>>>,
}
impl<T> Node<T> {
pub fn getLeft(&self) -> Option<&Self> {
if self.left.is_some() {
Some(&(*(self.left.unwrap())))
// Some(self.left.unwrap()) <= same result
}
None
}
}
fn main() {}
当我收到此错误时,似乎存在类型不匹配:
error[E0308]: mismatched types
--> src/main.rs:10:13
|
10 | Some(&(*(self.left.unwrap())))
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected (), found enum `std::option::Option`
|
= note: expected type `()`
found type `std::option::Option<&Node<T>>`
我是 Rust 新手,不明白为什么预期的类型是 () 而它应该是 Option<&Self> 以及如何返回对框内节点的引用(我如何才能移出借来的自己)?
【问题讨论】:
标签: rust borrow-checker