Skip to content

Commit 00738f5

Browse files
authored
idomatic rust (#1652)
* idomatic rust * Update linkedlist queue/deque
1 parent eec69f4 commit 00738f5

File tree

4 files changed

+29
-26
lines changed

4 files changed

+29
-26
lines changed

codes/rust/chapter_array_and_linkedlist/my_list.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,7 @@ struct MyList {
1919
impl MyList {
2020
/* 构造方法 */
2121
pub fn new(capacity: usize) -> Self {
22-
let mut vec = Vec::new();
23-
vec.resize(capacity, 0);
22+
let mut vec = vec![0; capacity];
2423
Self {
2524
arr: vec,
2625
capacity,
@@ -92,7 +91,7 @@ impl MyList {
9291
};
9392
let num = self.arr[index];
9493
// 将将索引 index 之后的元素都向前移动一位
95-
for j in (index..self.size - 1) {
94+
for j in index..self.size - 1 {
9695
self.arr[j] = self.arr[j + 1];
9796
}
9897
// 更新元素数量
@@ -111,7 +110,7 @@ impl MyList {
111110
}
112111

113112
/* 将列表转换为数组 */
114-
pub fn to_array(&mut self) -> Vec<i32> {
113+
pub fn to_array(&self) -> Vec<i32> {
115114
// 仅转换有效长度范围内的列表元素
116115
let mut arr = Vec::new();
117116
for i in 0..self.size {

codes/rust/chapter_stack_and_queue/linkedlist_deque.rs

+11-7
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ impl<T: Copy> LinkedListDeque<T> {
120120
}
121121
}
122122
self.que_size -= 1; // 更新队列长度
123-
Rc::try_unwrap(old_front).ok().unwrap().into_inner().val
123+
old_front.borrow().val
124124
})
125125
}
126126
// 队尾出队操作
@@ -136,7 +136,7 @@ impl<T: Copy> LinkedListDeque<T> {
136136
}
137137
}
138138
self.que_size -= 1; // 更新队列长度
139-
Rc::try_unwrap(old_rear).ok().unwrap().into_inner().val
139+
old_rear.borrow().val
140140
})
141141
}
142142
}
@@ -163,12 +163,16 @@ impl<T: Copy> LinkedListDeque<T> {
163163

164164
/* 返回数组用于打印 */
165165
pub fn to_array(&self, head: Option<&Rc<RefCell<ListNode<T>>>>) -> Vec<T> {
166-
if let Some(node) = head {
167-
let mut nums = self.to_array(node.borrow().next.as_ref());
168-
nums.insert(0, node.borrow().val);
169-
return nums;
166+
let mut res: Vec<T> = Vec::new();
167+
fn recur<T: Copy>(cur: Option<&Rc<RefCell<ListNode<T>>>>, res: &mut Vec<T>) {
168+
if let Some(cur) = cur {
169+
res.push(cur.borrow().val);
170+
recur(cur.borrow().next.as_ref(), res);
171+
}
170172
}
171-
return Vec::new();
173+
174+
recur(head, &mut res);
175+
res
172176
}
173177
}
174178

codes/rust/chapter_stack_and_queue/linkedlist_queue.rs

+12-6
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ impl<T: Copy> LinkedListQueue<T> {
6767
}
6868
}
6969
self.que_size -= 1;
70-
Rc::try_unwrap(old_front).ok().unwrap().into_inner().val
70+
old_front.borrow().val
7171
})
7272
}
7373

@@ -78,12 +78,18 @@ impl<T: Copy> LinkedListQueue<T> {
7878

7979
/* 将链表转化为 Array 并返回 */
8080
pub fn to_array(&self, head: Option<&Rc<RefCell<ListNode<T>>>>) -> Vec<T> {
81-
if let Some(node) = head {
82-
let mut nums = self.to_array(node.borrow().next.as_ref());
83-
nums.insert(0, node.borrow().val);
84-
return nums;
81+
let mut res: Vec<T> = Vec::new();
82+
83+
fn recur<T: Copy>(cur: Option<&Rc<RefCell<ListNode<T>>>>, res: &mut Vec<T>) {
84+
if let Some(cur) = cur {
85+
res.push(cur.borrow().val);
86+
recur(cur.borrow().next.as_ref(), res);
87+
}
8588
}
86-
return Vec::new();
89+
90+
recur(head, &mut res);
91+
92+
res
8793
}
8894
}
8995

codes/rust/chapter_stack_and_queue/linkedlist_stack.rs

+3-9
Original file line numberDiff line numberDiff line change
@@ -45,16 +45,10 @@ impl<T: Copy> LinkedListStack<T> {
4545
/* 出栈 */
4646
pub fn pop(&mut self) -> Option<T> {
4747
self.stack_peek.take().map(|old_head| {
48-
match old_head.borrow_mut().next.take() {
49-
Some(new_head) => {
50-
self.stack_peek = Some(new_head);
51-
}
52-
None => {
53-
self.stack_peek = None;
54-
}
55-
}
48+
self.stack_peek = old_head.borrow_mut().next.take();
5649
self.stk_size -= 1;
57-
Rc::try_unwrap(old_head).ok().unwrap().into_inner().val
50+
51+
old_head.borrow().val
5852
})
5953
}
6054

0 commit comments

Comments
 (0)