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