Skip to content

Commit

Permalink
Update flow-control.md
Browse files Browse the repository at this point in the history
去掉希望强调所有权的提示, 转为所有权说明.

如有错误请指正.
  • Loading branch information
acehinnnqru authored May 15, 2023
1 parent d17927f commit 00c2a73
Showing 1 changed file with 1 addition and 1 deletion.
2 changes: 1 addition & 1 deletion src/basic/flow-control.md
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ for item in collection {
第一种方式是循环索引,然后通过索引下标去访问集合,第二种方式是直接循环集合中的元素,优劣如下:

- **性能**:第一种使用方式中 `collection[index]` 的索引访问,会因为边界检查(Bounds Checking)导致运行时的性能损耗 —— Rust 会检查并确认 `index` 是否落在集合内,但是第二种直接迭代的方式就不会触发这种检查,因为编译器会在编译时就完成分析并证明这种访问是合法的
- **安全**:第一种方式里对 `collection` 的索引访问是非连续的,存在一定可能性在两次访问之间,`collection` 发生了变化,导致脏数据产生。而第二种直接迭代的方式是连续访问,因此不存在这种风险(这里是因为所有权吗?是的话可能要强调一下)
- **安全**:第一种方式里对 `collection` 的索引访问是非连续的,存在一定可能性在两次访问之间,`collection` 发生了变化,导致脏数据产生。而第二种直接迭代的方式是连续访问,因此不存在这种风险。同时因为所有权限制,这里访问的过程中,数据并不会发生变化。

由于 `for` 循环无需任何条件限制,也不需要通过索引来访问,因此是最安全也是最常用的,通过与下面的 `while` 的对比,我们能看到为什么 `for` 会更加安全。

Expand Down

0 comments on commit 00c2a73

Please sign in to comment.