mirror of
https://github.com/krahets/hello-algo.git
synced 2025-11-02 12:58:42 +08:00
[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:
@ -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];
|
||||
```
|
||||
|
||||
|
||||
Reference in New Issue
Block a user