遍历 BST 时借用时临时值下降
Temporary value dropped while borrowed when traversing a BST
use std::{cell::RefCell, rc::Rc};
#[derive(Debug, PartialEq, Eq)]
pub struct TreeNode {
pub val: i32,
pub left: Option<Rc<RefCell<TreeNode>>>,
pub right: Option<Rc<RefCell<TreeNode>>>,
}
fn find_min(mut node: &Option<Rc<RefCell<TreeNode>>>) -> i32 {
while node.as_ref().unwrap().borrow().left.is_some() {
node = &node.as_ref().unwrap().borrow().left;
}
node.as_ref().unwrap().borrow().val
}
error[E0716]: temporary value dropped while borrowed
--> src/lib.rs:12:17
|
10 | fn find_min(mut node: &Option<Rc<RefCell<TreeNode>>>) -> i32 {
| - let's call the lifetime of this reference `'1`
11 | while node.as_ref().unwrap().borrow().left.is_some() {
12 | node = &node.as_ref().unwrap().borrow().left;
| --------^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^------ temporary value is freed at the end of this statement
| | |
| | creates a temporary which is freed while still in use
| assignment requires that borrow lasts for `'1`
TreeNode
已给定且无法更改。
我明白为什么会出现错误,但不知道如何解决。
更新:
使用 Rc
作为参数看起来可能更好:
use std::rc::Rc;
#[derive(Debug, PartialEq, Eq)]
pub struct TreeNode {
pub val: i32,
pub left: Option<Rc<TreeNode>>,
pub right: Option<Rc<TreeNode>>,
}
pub fn find_min(mut current: Rc<TreeNode>) -> i32 {
while current.left.is_some() {
current = Rc::clone(current.left.as_ref().unwrap());
}
current.val
}
fn main() {
let left_child = TreeNode {
val: 1,
left: None,
right: None,
};
let root = TreeNode {
val: 6,
left: Some(Rc::new(left_child)),
right: None,
};
println!("min: {}", find_min(Rc::new(root)));
}
RefCell
和 mut
对我来说是多余的?
怎么样:
use std::rc::Rc;
#[derive(Debug, PartialEq, Eq)]
pub struct TreeNode {
pub val: i32,
pub left: Option<Rc<TreeNode>>,
pub right: Option<Rc<TreeNode>>,
}
pub fn find_min(root: &TreeNode) -> i32 {
let mut current = Rc::new(root);
while current.left.is_some() {
current = Rc::new(current.left.as_ref().unwrap());
}
current.val
}
根据写了我的解决方案:
fn find_min(node: &Option<Rc<RefCell<TreeNode>>>) -> i32 {
let mut curr = node.as_ref().unwrap().clone();
while curr.borrow().left.is_some() {
let tmp = curr.borrow().left.as_ref().unwrap().clone();
curr = tmp;
}
let res = curr.borrow().val;
res
}
两个临时变量看起来都不太好,但就是这样。
use std::{cell::RefCell, rc::Rc};
#[derive(Debug, PartialEq, Eq)]
pub struct TreeNode {
pub val: i32,
pub left: Option<Rc<RefCell<TreeNode>>>,
pub right: Option<Rc<RefCell<TreeNode>>>,
}
fn find_min(mut node: &Option<Rc<RefCell<TreeNode>>>) -> i32 {
while node.as_ref().unwrap().borrow().left.is_some() {
node = &node.as_ref().unwrap().borrow().left;
}
node.as_ref().unwrap().borrow().val
}
error[E0716]: temporary value dropped while borrowed
--> src/lib.rs:12:17
|
10 | fn find_min(mut node: &Option<Rc<RefCell<TreeNode>>>) -> i32 {
| - let's call the lifetime of this reference `'1`
11 | while node.as_ref().unwrap().borrow().left.is_some() {
12 | node = &node.as_ref().unwrap().borrow().left;
| --------^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^------ temporary value is freed at the end of this statement
| | |
| | creates a temporary which is freed while still in use
| assignment requires that borrow lasts for `'1`
TreeNode
已给定且无法更改。
我明白为什么会出现错误,但不知道如何解决。
更新:
使用 Rc
作为参数看起来可能更好:
use std::rc::Rc;
#[derive(Debug, PartialEq, Eq)]
pub struct TreeNode {
pub val: i32,
pub left: Option<Rc<TreeNode>>,
pub right: Option<Rc<TreeNode>>,
}
pub fn find_min(mut current: Rc<TreeNode>) -> i32 {
while current.left.is_some() {
current = Rc::clone(current.left.as_ref().unwrap());
}
current.val
}
fn main() {
let left_child = TreeNode {
val: 1,
left: None,
right: None,
};
let root = TreeNode {
val: 6,
left: Some(Rc::new(left_child)),
right: None,
};
println!("min: {}", find_min(Rc::new(root)));
}
RefCell
和 mut
对我来说是多余的?
怎么样:
use std::rc::Rc;
#[derive(Debug, PartialEq, Eq)]
pub struct TreeNode {
pub val: i32,
pub left: Option<Rc<TreeNode>>,
pub right: Option<Rc<TreeNode>>,
}
pub fn find_min(root: &TreeNode) -> i32 {
let mut current = Rc::new(root);
while current.left.is_some() {
current = Rc::new(current.left.as_ref().unwrap());
}
current.val
}
根据
fn find_min(node: &Option<Rc<RefCell<TreeNode>>>) -> i32 {
let mut curr = node.as_ref().unwrap().clone();
while curr.borrow().left.is_some() {
let tmp = curr.borrow().left.as_ref().unwrap().clone();
curr = tmp;
}
let res = curr.borrow().val;
res
}
两个临时变量看起来都不太好,但就是这样。