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