我正在尝试实现一个自平衡二叉搜索树并编写了一个函数来用它的左旋转替换一棵树:
struct BST<'a> {
l: Option<&'a BST<'a>>,
r: Option<&'a BST<'a>>
}
impl<'a> BST<'a> {
fn left_rotate(self) -> BST<'a> {
/*
* (x) (y)
* / \ / \
* a (y) => (x) c
* / \ / \
* b c a b
*/
match self.r {
None => self,
Some(y) => BST {
l: Some(& BST {l: self.l, r: y.l}),
r: y.r
}
}
}
}
尝试使用rustc bst.rs
导致以下错误编译此示例:
error: borrowed value does not live long enough
--> bst.rs:18:27
|
18 | l: Some(& BST {l: self.l, r: y.l}),
| ^^^^^^^^^^^^^^^^^^^^^^^ temporary value created here
19 | r: y.r
20 | }
| - temporary value only lives until here
|
note: borrowed value must be valid for the lifetime 'a as defined on the block at 7:36...
--> bst.rs:7:37
|
7 | fn left_rotate(self) -> BST<'a> {
| ^
我知道,由于函数返回时原始树被破坏,由于生命周期参数逆变,它的左旋转不能超过它。我的意图是让函数消耗原始树并返回左旋转,这样左旋转将继承原始树在未调用函数时的生命周期。这在 Rust 中可能吗?如果不是,实现我支持树替换的目标的最简单设计是什么?我的偏好是避免依赖 Rust 标准库并学会自己管理生命周期。
请原谅我缺乏 Rust 生命周期的经验。我的背景知识主要是 C++ 和 ML 风格的语言。