idomatic rust (#1652)

* idomatic rust

* Update linkedlist queue/deque
This commit is contained in:
rongyi
2025-02-10 10:40:29 +08:00
committed by GitHub
parent eec69f45af
commit 00738f5bb4
4 changed files with 29 additions and 26 deletions
@@ -19,8 +19,7 @@ struct MyList {
impl MyList {
/* 构造方法 */
pub fn new(capacity: usize) -> Self {
let mut vec = Vec::new();
vec.resize(capacity, 0);
let mut vec = vec![0; capacity];
Self {
arr: vec,
capacity,
@@ -92,7 +91,7 @@ impl MyList {
};
let num = self.arr[index];
// 将将索引 index 之后的元素都向前移动一位
for j in (index..self.size - 1) {
for j in index..self.size - 1 {
self.arr[j] = self.arr[j + 1];
}
// 更新元素数量
@@ -111,7 +110,7 @@ impl MyList {
}
/* 将列表转换为数组 */
pub fn to_array(&mut self) -> Vec<i32> {
pub fn to_array(&self) -> Vec<i32> {
// 仅转换有效长度范围内的列表元素
let mut arr = Vec::new();
for i in 0..self.size {