Unsize type must be greater than or equal to 0 (#931)

This commit is contained in:
易春风 2023-11-09 17:21:09 +08:00 committed by GitHub
parent 99fd891d76
commit c81d5e091b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -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) {