Skip to content

Commit 30e3bd1

Browse files
committed
feat(rust): Update "数组" in variable
1 parent f42d51b commit 30e3bd1

File tree

1 file changed

+23
-1
lines changed

1 file changed

+23
-1
lines changed

docs/languages/rust/variable.md

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,29 @@ let arr_long: [i32; 100] = [0; 100];
188188

189189
!!! note "与 C++ 的对照"
190190

191-
Rust 的数组可以直接作为参数传递,这一点与 C/C++ 不同(C++ 中的 `std::tuple` 与 Rust 的数组更加类似)。与 C/C++ 类似的是,数组的长度都是不可变的,变长的线性容器在两门语言中分别叫做 `std::Vec` 和 `std::vector`。
191+
Rust 的数组可以直接作为参数传递,这一点与 C/C++ 不同(C++ 中的 `std::tuple` 与 Rust 的数组更加类似)。这是因为 Rust 的数组是在栈上分配的。与 C/C++ 类似的是,数组的长度都是不可变的,变长的线性容器在两门语言中分别叫做 `std::Vec` 和 `std::vector`。
192+
193+
Rust 的下标访问自带越界检查。
194+
195+
下面的代码将会在编译时报错:
196+
197+
```rust
198+
let arr = [1, 2, 3];
199+
let element = arr[3]; // 编译器报错:index out of bounds: the length is 3 but the index is 3
200+
```
201+
202+
下面的代码能够通过编译,但当输入的下标超出数组长度时,程序会立即退出而不允许访问越界的内存:
203+
204+
```rust
205+
use std::io;
206+
207+
let arr = [1, 2, 3];
208+
let mut index = String::new();
209+
io::stdin().read_line(&mut index).expect("Failed to read line"); // 若输入10...
210+
let index: usize = index.trim().parse().expect("Index must be a number");
211+
let element = arr[index]; // ...程序产生运行时错误并退出
212+
println!("The value of element is: {}", element); // 该行不会被执行
213+
```
192214

193215
### 单元类型
194216

0 commit comments

Comments
 (0)