我正在尝试在Rust中编写二进制搜索树,但我不明白发生了什么:
enum BST<'a, T: Ord> {
Leaf,
BinTree { value: T, left: &'a mut BST<'a, T>, right: &'a mut BST<'a, T> }
}
impl<'a, T: Ord> BST<'a, T> {
fn new() -> BST<'a, T> {
BST::Leaf
}
fn add(self, val: T) {
match self {
BST::Leaf => self = BST::BinTree {
value: val,
left: &mut BST::<'a, T>::new(),
right: &mut BST::<'a, T>::new()
},
BST::BinTree{value: v, left: l, right: r} => if val < v {
l.add(val);
} else {
r.add(val);
}
}
}
}
fn main() {
}
当我尝试编译时,我收到以下错误:
error[E0309]: the parameter type `T` may not live long enough
--> heap.rs:3:25
|
3 | BinTree { value: T, left: &'a mut BST<'a, T>, right: &'a mut BST<'a, T> }
| ^^^^^^^^^^^^^^^^^^^^^^^^
|
= help: consider adding an explicit lifetime bound `T: 'a`...
note: ...so that the reference type `&'a mut BST<'a, T>` does not outlive the data it points at
--> heap.rs:3:25
|
3 | BinTree { value: T, left: &'a mut BST<'a, T>, right: &'a mut BST<'a, T> }
| ^^^^^^^^^^^^^^^^^^^^^^^^
好吧,经过大量的研究并做了编译器建议的事情,我想出了这段代码:
enum BST<'a, T: Ord + 'a> {
Leaf,
BinTree {
value: T,
left: &'a mut BST<'a, T>,
right: &'a mut BST<'a, T>
}
}
impl<'a, T: Ord + 'a > BST<'a, T> {
fn new() -> BST<'a, T> {
BST::Leaf
}
fn add(&mut self, val: T) {
match *self {
BST::Leaf => *self = BST::BinTree {
value: val,
left: &mut BST::<'a, T>::new() as &'a mut BST<'a, T>,
right: &mut BST::<'a, T>::new() as &'a mut BST<'a, T>
},
BST::BinTree{value: ref v, left: ref mut l, right: ref mut r} => if val < *v {
l.add(val);
} else {
r.add(val);
}
}
}
}
fn main() {
}
但我仍然会遇到错误:
error: borrowed value does not live long enough
--> heap.rs:19:16
|
19 | left: &mut BST::<'a, T>::new() as &'a mut BST<'a, T>,
| ^^^^^^^^^^^^^^^^^^^ does not live long enough
20 | right: &mut BST::<'a, T>::new() as &'a mut BST<'a, T>
21 | },
| - temporary value only lives until here
|
note: borrowed value must be valid for the lifetime 'a as defined on the body at 15:27...
--> heap.rs:15:28
|
15 | fn add(&mut self, val: T) {
| ____________________________^
16 | | match *self {
17 | | BST::Leaf => *self = BST::BinTree {
18 | | value: val,
... |
27 | | }
28 | | }
| |__^
error: borrowed value does not live long enough
--> heap.rs:20:17
|
20 | right: &mut BST::<'a, T>::new() as &'a mut BST<'a, T>
| ^^^^^^^^^^^^^^^^^^^ does not live long enough
21 | },
| - temporary value only lives until here
|
note: borrowed value must be valid for the lifetime 'a as defined on the body at 15:27...
--> heap.rs:15:28
|
15 | fn add(&mut self, val: T) {
| ____________________________^
16 | | match *self {
17 | | BST::Leaf => *self = BST::BinTree {
18 | | value: val,
... |
27 | | }
28 | | }
| |__^
error: aborting due to 2 previous errors
我知道这可以通过使用Box
而不是引用来修复,但我想让它像练习一样。
答案 0 :(得分:1)
如错误消息所示,可以通过添加生命周期绑定T: 'a
来修复该特定错误。但是你会得到许多其他错误,因为你要做的事情是不健全的:你试图存储对其他地方没有所有者的对象的引用。
当您执行诸如在节点中存储&mut BST::<'a, T>::new()
之类的操作时,BST::<'a, T>::new()
会返回一个临时值,该值很快就会被销毁,因此您无法存储对它的引用并期望它继续存在。
您需要您的节点拥有其子节点,而不是引用。您可以通过在创建新子节点时将子类型更改为left: Box<BST<T>>
并使用Box::new
来执行此操作。完成此操作后,您可以摆脱所有'a
无处不在的错误。
另一个问题是,您的add
会使用self
参数,因此调用者将无法再使用它。您应该改为使用&mut self
,以便它可以修改调用者拥有的树。