Skip to content

Commit

Permalink
[Rust] Use arrays instead of vectors in Chapter 4.1 Array (krahets#1357)
Browse files Browse the repository at this point in the history
* [Rust] Use array in chapter 4.1

* docs: update comments

* docs: update comments

* docs: update comments

* fix: update slices

* docs: update comments
  • Loading branch information
CarrotDLaw authored May 15, 2024
1 parent 840692a commit 9afbc9e
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 19 deletions.
18 changes: 10 additions & 8 deletions codes/rust/chapter_array_and_linkedlist/array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ fn random_access(nums: &[i32]) -> i32 {
}

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

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

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

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

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

Expand Down
7 changes: 6 additions & 1 deletion docs/chapter_array_and_linkedlist/array.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,12 @@

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

Expand Down
7 changes: 6 additions & 1 deletion en/docs/chapter_array_and_linkedlist/array.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,12 @@ Arrays can be initialized in two ways depending on the needs: either without ini

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

Expand Down
18 changes: 10 additions & 8 deletions zh-hant/codes/rust/chapter_array_and_linkedlist/array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ fn random_access(nums: &[i32]) -> i32 {
}

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

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

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

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

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

Expand Down
7 changes: 6 additions & 1 deletion zh-hant/docs/chapter_array_and_linkedlist/array.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,12 @@

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

Expand Down

0 comments on commit 9afbc9e

Please sign in to comment.