From 7e28f3ec8423c26005fe71bd09b1cdfc89c532a3 Mon Sep 17 00:00:00 2001 From: Colin Date: Tue, 22 Feb 2022 11:43:51 +0800 Subject: [PATCH 1/3] add a note about iterating an array The following snippet can run without an error: ```rust let mut a = [ 0, 1, 2, 3, 4 ]; let mut ind = 0; for num in a { println!("{}", num); if ind+1 < a.len() { a[ind+1] *= 10; } ind += 1; } println!("{:?}", a); ``` --- contents/basic/flow-control.md | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/contents/basic/flow-control.md b/contents/basic/flow-control.md index f997876c..c3db6e56 100644 --- a/contents/basic/flow-control.md +++ b/contents/basic/flow-control.md @@ -102,13 +102,16 @@ for 元素 in 集合 { ``` 这个语法跟 JavaScript 还蛮像,应该挺好理解。 -注意,使用 `for` 时我们往往使用集合的引用形式,除非你不想在后面的代码中继续使用该集合(比如我们这里使用了 `container` 的引用)。如果不使用引用的话,所有权会被转移到 `for` 语句块中,后面就无法再使用这个集合了): +注意,使用 `for` 时我们往往使用集合的引用形式,除非你不想在后面的代码中继续使用该集合(比如我们这里使用了 `container` 的引用)。如果不使用引用的话,所有权会被转移(move)到 `for` 语句块中,后面就无法再使用这个集合了): ```rust for item in &container { // ... } ``` +【注:相比集合(collection),对于实现了 `copy` 特征的数组(array)而言, `for item in arr` 并不会把 `arr` 转移,因此循环之后仍然可以使用 `arr` 。】 + + 如果想在循环中,**修改该元素**,可以使用 `mut` 关键字: ```rust for item in &mut collection { From 22bfdd985f61e21aeb908fcb9842328c84da00f1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BB=98=E5=BB=BA=E9=82=A6?= Date: Tue, 22 Feb 2022 11:48:19 +0800 Subject: [PATCH 2/3] fix: move conclusion --- contents/basic/formatted-output.md | 2 ++ contents/basic/result-error/result.md | 3 --- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/contents/basic/formatted-output.md b/contents/basic/formatted-output.md index c36fa0c1..955c8baa 100644 --- a/contents/basic/formatted-output.md +++ b/contents/basic/formatted-output.md @@ -414,3 +414,5 @@ note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace 还是那句话,[<>](https://github.com/sunface/rust-course)不仅仅是 Rust 学习书籍,还是一本厚重的工具书! +至此,Rust 的基础内容学习已经全部完成,下面我们将学习 Rust 的高级进阶内容,正式开启你的高手之路。 + diff --git a/contents/basic/result-error/result.md b/contents/basic/result-error/result.md index 88117b90..c0342e5d 100644 --- a/contents/basic/result-error/result.md +++ b/contents/basic/result-error/result.md @@ -344,6 +344,3 @@ let x = try!(function_with_error()); 总之,`try!` 作为前浪已经死在了沙滩上,**在当前版本中,我们要尽量避免使用 try!**。 -至此,Rust 的基础内容学习已经全部完成,下面我们将学习 Rust 的高级进阶内容,正式开启你的高手之路。 - - From 9096647083f70c430209e2ee5d0a535250342b60 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=AD=99=E9=A3=9ESunface?= Date: Tue, 22 Feb 2022 12:22:47 +0800 Subject: [PATCH 3/3] Update contents/basic/flow-control.md --- contents/basic/flow-control.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/contents/basic/flow-control.md b/contents/basic/flow-control.md index c3db6e56..9ee95b55 100644 --- a/contents/basic/flow-control.md +++ b/contents/basic/flow-control.md @@ -109,7 +109,7 @@ for item in &container { } ``` -【注:相比集合(collection),对于实现了 `copy` 特征的数组(array)而言, `for item in arr` 并不会把 `arr` 转移,因此循环之后仍然可以使用 `arr` 。】 +> 对于实现了 `copy` 特征的数组(例如 [i32; 10] )而言, `for item in arr` 并不会把 `arr` 转移,而是直接对其进行了拷贝,因此循环之后仍然可以使用 `arr` 。 如果想在循环中,**修改该元素**,可以使用 `mut` 关键字: