Skip to content

Commit 4fb9321

Browse files
committed
Polish rust
1 parent 6a4d624 commit 4fb9321

4 files changed

Lines changed: 14 additions & 20 deletions

File tree

codes/rust/chapter_array_and_linkedlist/my_list.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ impl MyList {
9090
panic!("索引越界")
9191
};
9292
let num = self.arr[index];
93-
// 将将索引 index 之后的元素都向前移动一位
93+
// 将索引 index 之后的元素都向前移动一位
9494
for j in index..self.size - 1 {
9595
self.arr[j] = self.arr[j + 1];
9696
}

codes/rust/chapter_heap/heap.rs

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,22 +13,12 @@ fn test_push_max(heap: &mut BinaryHeap<i32>, val: i32) {
1313
println!("\n元素 {} 入堆后", val);
1414
print_util::print_heap(heap.iter().map(|&val| val).collect());
1515
}
16-
fn test_push_min(heap: &mut BinaryHeap<Reverse<i32>>, val: i32) {
17-
heap.push(Reverse(val)); // 元素入堆
18-
println!("\n元素 {} 入堆后", val);
19-
print_util::print_heap(heap.iter().map(|&val| val.0).collect());
20-
}
2116

2217
fn test_pop_max(heap: &mut BinaryHeap<i32>) {
2318
let val = heap.pop().unwrap();
2419
println!("\n堆顶元素 {} 出堆后", val);
2520
print_util::print_heap(heap.iter().map(|&val| val).collect());
2621
}
27-
fn test_pop_min(heap: &mut BinaryHeap<Reverse<i32>>) {
28-
let val = heap.pop().unwrap().0;
29-
println!("\n堆顶元素 {} 出堆后", val);
30-
print_util::print_heap(heap.iter().map(|&val| val.0).collect());
31-
}
3222

3323
/* Driver Code */
3424
fn main() {

codes/rust/chapter_stack_and_queue/linkedlist_stack.rs

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -58,13 +58,17 @@ impl<T: Copy> LinkedListStack<T> {
5858
}
5959

6060
/* 将 List 转化为 Array 并返回 */
61-
pub fn to_array(&self, head: Option<&Rc<RefCell<ListNode<T>>>>) -> Vec<T> {
62-
if let Some(node) = head {
63-
let mut nums = self.to_array(node.borrow().next.as_ref());
64-
nums.push(node.borrow().val);
65-
return nums;
61+
pub fn to_array(&self) -> Vec<T> {
62+
fn _to_array<T: Sized + Copy>(head: Option<&Rc<RefCell<ListNode<T>>>>) -> Vec<T> {
63+
if let Some(node) = head {
64+
let mut nums = _to_array(node.borrow().next.as_ref());
65+
nums.push(node.borrow().val);
66+
return nums;
67+
}
68+
return Vec::new();
6669
}
67-
return Vec::new();
70+
71+
_to_array(self.peek())
6872
}
6973
}
7074

@@ -80,7 +84,7 @@ fn main() {
8084
stack.push(5);
8185
stack.push(4);
8286
print!("栈 stack = ");
83-
print_util::print_array(&stack.to_array(stack.peek()));
87+
print_util::print_array(&stack.to_array());
8488

8589
/* 访问栈顶元素 */
8690
let peek = stack.peek().unwrap().borrow().val;
@@ -89,7 +93,7 @@ fn main() {
8993
/* 元素出栈 */
9094
let pop = stack.pop().unwrap();
9195
print!("\n出栈元素 pop = {},出栈后 stack = ", pop);
92-
print_util::print_array(&stack.to_array(stack.peek()));
96+
print_util::print_array(&stack.to_array());
9397

9498
/* 获取栈的长度 */
9599
let size = stack.size();

codes/rust/src/include/tree_node.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ impl TreeNode {
3434
macro_rules! op_vec {
3535
( $( $x:expr ),* ) => {
3636
vec![
37-
$( Option::from($x).map(|x| x) ),*
37+
$(Option::from($x)),*
3838
]
3939
};
4040
}

0 commit comments

Comments
 (0)