File tree Expand file tree Collapse file tree 1 file changed +23
-1
lines changed Expand file tree Collapse file tree 1 file changed +23
-1
lines changed Original file line number Diff line number Diff line change @@ -188,7 +188,29 @@ let arr_long: [i32; 100] = [0; 100];
188
188
189
189
!!! note "与 C++ 的对照"
190
190
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
+ ```
192
214
193
215
### 单元类型
194
216
You can’t perform that action at this time.
0 commit comments