[Rust] Use arrays instead of vectors in Chapter 4.1 Array (#1357)

* [Rust] Use array in chapter 4.1

* docs: update comments

* docs: update comments

* docs: update comments

* fix: update slices

* docs: update comments
This commit is contained in:
CarrotDLaw
2024-05-15 18:31:48 +08:00
committed by GitHub
parent 840692acce
commit 9afbc9eda5
5 changed files with 38 additions and 19 deletions

View File

@ -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];
```