我想通过一系列插入来创建一个二叉树。每个插入都提供一个u8
和一个值的列表。 u8
的列表代表树下降时的左侧或右侧:0代表左,而不是0代表右。
尽管有number of examples个Rust二进制树,但我还没有遇到通过循环而不是递归插入的树。我来了,陷入了以下问题(playground):
pub struct Tree {
data: u8,
left: Option<Box<Tree>>,
right: Option<Box<Tree>>,
}
impl Tree {
fn new() -> Self {
Tree {
data: 0,
left: None,
right: None,
}
}
fn insert(&mut self, code: &[u8], data: u8) {
let mut tree = self;
for i in code {
match i {
0 => {
if tree.left.is_none() {
tree.left = Some(Box::new(Tree::new()));
}
tree = &mut tree.left.as_mut().unwrap(); // <---
}
_ => {
if tree.right.is_none() {
tree.right = Some(Box::new(Tree::new()));
}
tree = &mut tree.right.unwrap(); // <---
}
}
}
tree.data = data;
}
}
我被困在确定的两个位置上,这些位置在我下降时尝试迭代分配tree
:
error[E0597]: borrowed value does not live long enough
--> src/main.rs:24:33
|
24 | tree = &mut tree.left.as_mut().unwrap();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^- temporary value dropped here while still borrowed
| |
| temporary value does not live long enough
...
35 | }
| - temporary value needs to live until here
|
= note: consider using a `let` binding to increase its lifetime
error[E0597]: borrowed value does not live long enough
--> src/main.rs:30:33
|
30 | tree = &mut tree.right.unwrap();
| ^^^^^^^^^^^^^^^^^^^- temporary value dropped here while still borrowed
| |
| temporary value does not live long enough
...
35 | }
| - temporary value needs to live until here
|
= note: consider using a `let` binding to increase its lifetime
error[E0507]: cannot move out of borrowed content
--> src/main.rs:30:33
|
30 | tree = &mut tree.right.unwrap();
| ^^^^ cannot move out of borrowed content
error[E0502]: cannot borrow `tree.left` as immutable because it is also borrowed as mutable
--> src/main.rs:21:24
|
21 | if tree.left.is_none() {
| ^^^^^^^^^ immutable borrow occurs here
...
24 | tree = &mut tree.left.as_mut().unwrap();
| --------- mutable borrow occurs here
...
35 | }
| - mutable borrow ends here
error[E0506]: cannot assign to `tree.left` because it is borrowed
--> src/main.rs:22:25
|
22 | tree.left = Some(Box::new(Tree::new()));
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ assignment to borrowed `tree.left` occurs here
23 | }
24 | tree = &mut tree.left.as_mut().unwrap();
| --------- borrow of `tree.left` occurs here
error[E0506]: cannot assign to `tree` because it is borrowed
--> src/main.rs:24:21
|
24 | tree = &mut tree.left.as_mut().unwrap();
| ^^^^^^^^^^^^---------^^^^^^^^^^^^^^^^^^
| | |
| | borrow of `tree` occurs here
| assignment to borrowed `tree` occurs here
error[E0499]: cannot borrow `tree.left` as mutable more than once at a time
--> src/main.rs:24:33
|
24 | tree = &mut tree.left.as_mut().unwrap();
| ^^^^^^^^^ mutable borrow starts here in previous iteration of loop
...
35 | }
| - mutable borrow ends here
error[E0506]: cannot assign to `tree` because it is borrowed
--> src/main.rs:30:21
|
24 | tree = &mut tree.left.as_mut().unwrap();
| --------- borrow of `tree` occurs here
...
30 | tree = &mut tree.right.unwrap();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ assignment to borrowed `tree` occurs here
解决这个问题的正确方法是什么?