Skip to content

Commit 9afbc9e

Browse files
authored
[Rust] Use arrays instead of vectors in Chapter 4.1 Array (krahets#1357)
* [Rust] Use array in chapter 4.1 * docs: update comments * docs: update comments * docs: update comments * fix: update slices * docs: update comments
1 parent 840692a commit 9afbc9e

File tree

5 files changed

+38
-19
lines changed
  • codes/rust/chapter_array_and_linkedlist
  • docs/chapter_array_and_linkedlist
  • en/docs/chapter_array_and_linkedlist
  • zh-hant
    • codes/rust/chapter_array_and_linkedlist
    • docs/chapter_array_and_linkedlist

5 files changed

+38
-19
lines changed

codes/rust/chapter_array_and_linkedlist/array.rs

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ fn random_access(nums: &[i32]) -> i32 {
1818
}
1919

2020
/* 扩展数组长度 */
21-
fn extend(nums: Vec<i32>, enlarge: usize) -> Vec<i32> {
21+
fn extend(nums: &[i32], enlarge: usize) -> Vec<i32> {
2222
// 初始化一个扩展长度后的数组
2323
let mut res: Vec<i32> = vec![0; nums.len() + enlarge];
2424
// 将原数组中的所有元素复制到新
@@ -30,7 +30,7 @@ fn extend(nums: Vec<i32>, enlarge: usize) -> Vec<i32> {
3030
}
3131

3232
/* 在数组的索引 index 处插入元素 num */
33-
fn insert(nums: &mut Vec<i32>, num: i32, index: usize) {
33+
fn insert(nums: &mut [i32], num: i32, index: usize) {
3434
// 把索引 index 以及之后的所有元素向后移动一位
3535
for i in (index + 1..nums.len()).rev() {
3636
nums[i] = nums[i - 1];
@@ -40,7 +40,7 @@ fn insert(nums: &mut Vec<i32>, num: i32, index: usize) {
4040
}
4141

4242
/* 删除索引 index 处的元素 */
43-
fn remove(nums: &mut Vec<i32>, index: usize) {
43+
fn remove(nums: &mut [i32], index: usize) {
4444
// 把索引 index 之后的所有元素向前移动一位
4545
for i in index..nums.len() - 1 {
4646
nums[i] = nums[i + 1];
@@ -73,13 +73,15 @@ fn find(nums: &[i32], target: i32) -> Option<usize> {
7373
/* Driver Code */
7474
fn main() {
7575
/* 初始化数组 */
76-
let arr = [0; 5];
76+
let arr: [i32; 5] = [0; 5];
77+
let slice: &[i32] = &[0; 5];
7778
print!("数组 arr = ");
7879
print_util::print_array(&arr);
79-
// 在 Rust 中,指定长度时([i32; 5])为数组
80+
// 在 Rust 中,指定长度时([i32; 5])为数组,不指定长度时(&[i32])为切片
8081
// 由于 Rust 的数组被设计为在编译期确定长度,因此只能使用常量来指定长度
81-
// 为了方便实现扩容 extend() 方法,以下将(Vec) 看作数组(Array)也是rust一般情况下使用动态数组的类型
82-
let nums = vec![1, 3, 2, 5, 4];
82+
// Vector 是 Rust 一般情况下用作动态数组的类型
83+
// 为了方便实现扩容 extend() 方法,以下将 vector 看作数组(array)
84+
let nums: Vec<i32> = vec![1, 3, 2, 5, 4];
8385
print!("\n数组 nums = ");
8486
print_util::print_array(&nums);
8587

@@ -88,7 +90,7 @@ fn main() {
8890
println!("\n在 nums 中获取随机元素 {}", random_num);
8991

9092
// 长度扩展
91-
let mut nums = extend(nums, 3);
93+
let mut nums: Vec<i32> = extend(&nums, 3);
9294
print!("将数组长度扩展至 8 ,得到 nums = ");
9395
print_util::print_array(&nums);
9496

docs/chapter_array_and_linkedlist/array.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,12 @@
9393

9494
```rust title="array.rs"
9595
/* 初始化数组 */
96-
let arr: Vec<i32> = vec![0; 5]; // [0, 0, 0, 0, 0]
96+
let arr: [i32; 5] = [0; 5]; // [0, 0, 0, 0, 0]
97+
let slice: &[i32] = &[0; 5];
98+
// 在 Rust 中,指定长度时([i32; 5])为数组,不指定长度时(&[i32])为切片
99+
// 由于 Rust 的数组被设计为在编译期确定长度,因此只能使用常量来指定长度
100+
// Vector 是 Rust 一般情况下用作动态数组的类型
101+
// 为了方便实现扩容 extend() 方法,以下将 vector 看作数组(array)
97102
let nums: Vec<i32> = vec![1, 3, 2, 5, 4];
98103
```
99104

en/docs/chapter_array_and_linkedlist/array.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,12 @@ Arrays can be initialized in two ways depending on the needs: either without ini
9393

9494
```rust title="array.rs"
9595
/* Initialize array */
96-
let arr: Vec<i32> = vec![0; 5]; // [0, 0, 0, 0, 0]
96+
let arr: [i32; 5] = [0; 5]; // [0, 0, 0, 0, 0]
97+
let slice: &[i32] = &[0; 5];
98+
// In Rust, specifying the length ([i32; 5]) denotes an array, while not specifying it (&[i32]) denotes a slice.
99+
// Since Rust's arrays are designed to have compile-time fixed length, only constants can be used to specify the length.
100+
// Vectors are generally used as dynamic arrays in Rust.
101+
// For convenience in implementing the extend() method, the vector will be considered as an array here.
97102
let nums: Vec<i32> = vec![1, 3, 2, 5, 4];
98103
```
99104

zh-hant/codes/rust/chapter_array_and_linkedlist/array.rs

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ fn random_access(nums: &[i32]) -> i32 {
1818
}
1919

2020
/* 擴展陣列長度 */
21-
fn extend(nums: Vec<i32>, enlarge: usize) -> Vec<i32> {
21+
fn extend(nums: &[i32], enlarge: usize) -> Vec<i32> {
2222
// 初始化一個擴展長度後的陣列
2323
let mut res: Vec<i32> = vec![0; nums.len() + enlarge];
2424
// 將原陣列中的所有元素複製到新
@@ -30,7 +30,7 @@ fn extend(nums: Vec<i32>, enlarge: usize) -> Vec<i32> {
3030
}
3131

3232
/* 在陣列的索引 index 處插入元素 num */
33-
fn insert(nums: &mut Vec<i32>, num: i32, index: usize) {
33+
fn insert(nums: &mut [i32], num: i32, index: usize) {
3434
// 把索引 index 以及之後的所有元素向後移動一位
3535
for i in (index + 1..nums.len()).rev() {
3636
nums[i] = nums[i - 1];
@@ -40,7 +40,7 @@ fn insert(nums: &mut Vec<i32>, num: i32, index: usize) {
4040
}
4141

4242
/* 刪除索引 index 處的元素 */
43-
fn remove(nums: &mut Vec<i32>, index: usize) {
43+
fn remove(nums: &mut [i32], index: usize) {
4444
// 把索引 index 之後的所有元素向前移動一位
4545
for i in index..nums.len() - 1 {
4646
nums[i] = nums[i + 1];
@@ -73,13 +73,15 @@ fn find(nums: &[i32], target: i32) -> Option<usize> {
7373
/* Driver Code */
7474
fn main() {
7575
/* 初始化陣列 */
76-
let arr = [0; 5];
76+
let arr: [i32; 5] = [0; 5];
77+
let slice: &[i32] = &[0; 5];
7778
print!("陣列 arr = ");
7879
print_util::print_array(&arr);
79-
// 在 Rust 中,指定長度時([i32; 5])為陣列
80+
// 在 Rust 中,指定長度时([i32; 5])爲陣列,不指定長度時(&[i32])爲切片
8081
// 由於 Rust 的陣列被設計為在編譯期確定長度,因此只能使用常數來指定長度
81-
// 為了方便實現擴容 extend() 方法,以下將(Vec) 看作陣列(Array)也是rust一般情況下使用動態陣列的型別
82-
let nums = vec![1, 3, 2, 5, 4];
82+
// Vector 是 Rust 一般情況下用作動態陣列的類型
83+
// 為了方便實現擴容 extend() 方法,以下將 vector 看作陣列(array)
84+
let nums: Vec<i32> = vec![1, 3, 2, 5, 4];
8385
print!("\n陣列 nums = ");
8486
print_util::print_array(&nums);
8587

@@ -88,7 +90,7 @@ fn main() {
8890
println!("\n在 nums 中獲取隨機元素 {}", random_num);
8991

9092
// 長度擴展
91-
let mut nums = extend(nums, 3);
93+
let mut nums: Vec<i32> = extend(&nums, 3);
9294
print!("將陣列長度擴展至 8 ,得到 nums = ");
9395
print_util::print_array(&nums);
9496

zh-hant/docs/chapter_array_and_linkedlist/array.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,12 @@
9393

9494
```rust title="array.rs"
9595
/* 初始化陣列 */
96-
let arr: Vec<i32> = vec![0; 5]; // [0, 0, 0, 0, 0]
96+
let arr: [i32; 5] = [0; 5]; // [0, 0, 0, 0, 0]
97+
let slice: &[i32] = &[0; 5];
98+
// 在 Rust 中,指定長度时([i32; 5])爲陣列,不指定長度時(&[i32])爲切片
99+
// 由於 Rust 的陣列被設計為在編譯期確定長度,因此只能使用常數來指定長度
100+
// Vector 是 Rust 一般情況下用作動態陣列的類型
101+
// 為了方便實現擴容 extend() 方法,以下將 vector 看作陣列(array)
97102
let nums: Vec<i32> = vec![1, 3, 2, 5, 4];
98103
```
99104

0 commit comments

Comments
 (0)