fix(Rust): no need to clone in hash put fn (#1226)

* no need to clone in hash put fn

* fmt rust code

* make code more readable

* Change vec append to extend, more friendly to rookie rust dev

* drop comment
This commit is contained in:
rongyi
2024-04-07 14:46:21 +08:00
committed by GitHub
parent bd54594a90
commit f901a31bae
4 changed files with 16 additions and 18 deletions

View File

@ -25,7 +25,7 @@ impl TreeNode {
height: 0,
parent: None,
left: None,
right: None
right: None,
}))
}
}
@ -34,7 +34,7 @@ impl TreeNode {
macro_rules! op_vec {
( $( $x:expr ),* ) => {
vec![
$( Option::from($x).map(|x| x) ),*
$( Option::from($x).map(|x| x) ),*
]
};
}
@ -75,9 +75,10 @@ pub fn vec_to_tree(arr: Vec<Option<i32>>) -> Option<Rc<RefCell<TreeNode>>> {
fn tree_to_vec_dfs(root: Option<Rc<RefCell<TreeNode>>>, i: usize, res: &mut Vec<Option<i32>>) {
if root.is_none() {
return;
}
}
let root = root.unwrap();
while i >= res.len() {
// i + 1 is the minimum valid size to access index i
while res.len() < i + 1 {
res.push(None);
}
res[i] = Some(root.borrow().val);