From c81d5e091b7944d3032997d99901a81d7e9eb508 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=98=93=E6=98=A5=E9=A3=8E?= <77157236+night-cruise@users.noreply.github.com> Date: Thu, 9 Nov 2023 17:21:09 +0800 Subject: [PATCH] Unsize type must be greater than or equal to 0 (#931) --- codes/rust/chapter_array_and_linkedlist/my_list.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/codes/rust/chapter_array_and_linkedlist/my_list.rs b/codes/rust/chapter_array_and_linkedlist/my_list.rs index 2e3076719..efc6138ae 100644 --- a/codes/rust/chapter_array_and_linkedlist/my_list.rs +++ b/codes/rust/chapter_array_and_linkedlist/my_list.rs @@ -42,13 +42,13 @@ impl MyList { /* 访问元素 */ pub fn get(&self, index: usize) -> i32 { // 索引如果越界则抛出异常,下同 - if index < 0 || index >= self.size {panic!("索引越界")}; + if index >= self.size {panic!("索引越界")}; return self.arr[index]; } /* 更新元素 */ pub fn set(&mut self, index: usize, num: i32) { - if index < 0 || index >= self.size {panic!("索引越界")}; + if index >= self.size {panic!("索引越界")}; self.arr[index] = num; } @@ -65,7 +65,7 @@ impl MyList { /* 中间插入元素 */ pub fn insert(&mut self, index: usize, num: i32) { - if index < 0 || index >= self.size() {panic!("索引越界")}; + if index >= self.size() {panic!("索引越界")}; // 元素数量超出容量时,触发扩容机制 if self.size == self.capacity() { self.extend_capacity(); @@ -81,7 +81,7 @@ impl MyList { /* 删除元素 */ pub fn remove(&mut self, index: usize) -> i32 { - if index < 0 || index >= self.size() {panic!("索引越界")}; + if index >= self.size() {panic!("索引越界")}; let num = self.arr[index]; // 将索引 index 之后的元素都向前移动一位 for j in (index..self.size - 1) {