From 19e6a69de88a4c8543ea69de6c54308426bb4265 Mon Sep 17 00:00:00 2001 From: mg-chao Date: Tue, 4 Jan 2022 11:18:13 +0800 Subject: [PATCH] translate: move_semantics --- exercise/exercises/move_semantics/README.md | 8 +-- .../move_semantics/move_semantics1.rs | 4 +- .../move_semantics/move_semantics2.rs | 6 +- .../move_semantics/move_semantics3.rs | 6 +- .../move_semantics/move_semantics4.rs | 9 ++- .../move_semantics/move_semantics5.rs | 5 +- exercise/info.toml | 61 ++++++++----------- 7 files changed, 45 insertions(+), 54 deletions(-) diff --git a/exercise/exercises/move_semantics/README.md b/exercise/exercises/move_semantics/README.md index 54ddd8e6..ec50ba1d 100644 --- a/exercise/exercises/move_semantics/README.md +++ b/exercise/exercises/move_semantics/README.md @@ -1,10 +1,10 @@ -# Move Semantics +# 移动语义(Move Semantics) -These exercises are adapted from [pnkfelix](https://github.com/pnkfelix)'s [Rust Tutorial](https://pnkfelix.github.io/rust-examples-icfp2014/) -- Thank you Felix!!! +这些练习改编自 [pnkfelix](https://github.com/pnkfelix) 的 [Rust Tutorial](https://pnkfelix.github.io/rust-examples-icfp2014/) -- 谢谢 Felix !!! -## Further information +## 更多信息 -For this section, the book links are especially important. +以下书籍中的内容对于当前的学习尤其重要。 - [Ownership](https://doc.rust-lang.org/book/ch04-01-what-is-ownership.html) - [Reference and borrowing](https://doc.rust-lang.org/book/ch04-02-references-and-borrowing.html) diff --git a/exercise/exercises/move_semantics/move_semantics1.rs b/exercise/exercises/move_semantics/move_semantics1.rs index e2f5876d..69abae9c 100644 --- a/exercise/exercises/move_semantics/move_semantics1.rs +++ b/exercise/exercises/move_semantics/move_semantics1.rs @@ -1,5 +1,5 @@ // move_semantics1.rs -// Make me compile! Execute `rustlings hint move_semantics1` for hints :) +// 让我能够编译!执行 `rustex hint move_semantics1` 获取提示 :) // I AM NOT DONE @@ -8,7 +8,7 @@ fn main() { let vec1 = fill_vec(vec0); - println!("{} has length {} content `{:?}`", "vec1", vec1.len(), vec1); + println!("{} has length {} content `{:?}`", "vec1", vec1.len(), vec1);// 译:"{} 长度为 {} 内容是 `{:?}`" vec1.push(88); diff --git a/exercise/exercises/move_semantics/move_semantics2.rs b/exercise/exercises/move_semantics/move_semantics2.rs index bd21fbb7..ff52e032 100644 --- a/exercise/exercises/move_semantics/move_semantics2.rs +++ b/exercise/exercises/move_semantics/move_semantics2.rs @@ -1,6 +1,6 @@ // move_semantics2.rs -// Make me compile without changing line 13! -// Execute `rustlings hint move_semantics2` for hints :) +// 在不更改第 13 行的要求下通过编译! +// 执行 `rustex hint move_semantics2` 获取提示 :) // I AM NOT DONE @@ -9,7 +9,7 @@ fn main() { let mut vec1 = fill_vec(vec0); - // Do not change the following line! + // 不要更改下面那行! println!("{} has length {} content `{:?}`", "vec0", vec0.len(), vec0); vec1.push(88); diff --git a/exercise/exercises/move_semantics/move_semantics3.rs b/exercise/exercises/move_semantics/move_semantics3.rs index 43fef74f..d9fea4e2 100644 --- a/exercise/exercises/move_semantics/move_semantics3.rs +++ b/exercise/exercises/move_semantics/move_semantics3.rs @@ -1,7 +1,7 @@ // move_semantics3.rs -// Make me compile without adding new lines-- just changing existing lines! -// (no lines with multiple semicolons necessary!) -// Execute `rustlings hint move_semantics3` for hints :) +// 在不添加新行仅改变已有行的要求下通过编译! +// (也不允许有多个分号的行!) +// 执行 `rustex hint move_semantics3` 获取提示 :) // I AM NOT DONE diff --git a/exercise/exercises/move_semantics/move_semantics4.rs b/exercise/exercises/move_semantics/move_semantics4.rs index 2a23c710..8f7f0d52 100644 --- a/exercise/exercises/move_semantics/move_semantics4.rs +++ b/exercise/exercises/move_semantics/move_semantics4.rs @@ -1,8 +1,7 @@ // move_semantics4.rs -// Refactor this code so that instead of having `vec0` and creating the vector -// in `fn main`, we create it within `fn fill_vec` and transfer the -// freshly created vector from fill_vec to its caller. -// Execute `rustlings hint move_semantics4` for hints! +// 重构这段代码,做到删除 `vec0` ,并在 `fn fill_vec` 而非 `fn main` 中创建 vector , +// 然后将新创建的 vector 从 `fill_vec` 转移到其调用者。 +// 执行 `rustex hint move_semantics4` 获取提示 :) // I AM NOT DONE @@ -18,7 +17,7 @@ fn main() { println!("{} has length {} content `{:?}`", "vec1", vec1.len(), vec1); } -// `fill_vec()` no longer takes `vec: Vec` as argument +// `fill_vec()` 不再获取 `vec: Vec` 参数 fn fill_vec() -> Vec { let mut vec = vec; diff --git a/exercise/exercises/move_semantics/move_semantics5.rs b/exercise/exercises/move_semantics/move_semantics5.rs index c4704f9e..d57a1261 100644 --- a/exercise/exercises/move_semantics/move_semantics5.rs +++ b/exercise/exercises/move_semantics/move_semantics5.rs @@ -1,7 +1,6 @@ // move_semantics5.rs -// Make me compile only by reordering the lines in `main()`, but without -// adding, changing or removing any of them. -// Execute `rustlings hint move_semantics5` for hints :) +// 只通过重新排列 `main()` 中的已有行来完成编译,并且不能增加、更改或删除任何行 +// 执行 `rustex hint move_semantics5` 获取提示 :) // I AM NOT DONE diff --git a/exercise/info.toml b/exercise/info.toml index 99833a3e..88c9ae5d 100644 --- a/exercise/info.toml +++ b/exercise/info.toml @@ -152,64 +152,57 @@ name = "move_semantics1" path = "exercises/move_semantics/move_semantics1.rs" mode = "compile" hint = """ -So you've got the "cannot borrow immutable local variable `vec1` as mutable" error on line 13, -right? The fix for this is going to be adding one keyword, and the addition is NOT on line 13 -where the error is.""" +在第 13 行有个 "cannot borrow immutable local variable `vec1` as mutable"* 错误,对吗? +修复错误的方法是添加一个关键词,并且添加的位置不在报错的第 13 行上。 + +译注:不能将不可变的局部变量 `vec1` 借用为可变变量""" [[exercises]] name = "move_semantics2" path = "exercises/move_semantics/move_semantics2.rs" mode = "compile" hint = """ -So `vec0` is being *moved* into the function `fill_vec` when we call it on -line 10, which means it gets dropped at the end of `fill_vec`, which means we -can't use `vec0` again on line 13 (or anywhere else in `main` after the -`fill_vec` call for that matter). We could fix this in a few ways, try them -all! -1. Make another, separate version of the data that's in `vec0` and pass that - to `fill_vec` instead. -2. Make `fill_vec` borrow its argument instead of taking ownership of it, - and then copy the data within the function in order to return an owned - `Vec` -3. Make `fill_vec` *mutably* borrow its argument (which will need to be - mutable), modify it directly, then not return anything. Then you can get rid - of `vec1` entirely -- note that this will change what gets printed by the - first `println!`""" +当我们在第 10 行调用 `fill_vec` 时,`vec0' 被 *移动(moved)* 到 +函数 `fill_vec` 中,这意味着它会在 `fill_vec` 函数的末尾被丢弃,同时也 +导致了我们不能在第 13 行再次使用 `vec0`(或在 `main` 中调用 `fill_vec` 后的任何地方)。 +我们可以用几种方法来解决这个问题,都试一试吧! +1. 做一个 `vec0` 数据的拷贝,并将其传递给 `fill_vec` 。 +2. 让 `fill_vec` 通过借用而不是获取所有权的方式获取参数,然后在函数中复制一份数据,以便返回 + 一个具有所有权的 `Vec` 变量。 +3. 让 `fill_vec` 借用可变参数(参数也需要可变),直接进行操作,然后不返回任何东西。接着你需要 + 完全地去掉 `vec1`——但注意,这也将改变第一个 `println!` 打印出内容。""" [[exercises]] name = "move_semantics3" path = "exercises/move_semantics/move_semantics3.rs" mode = "compile" hint = """ -The difference between this one and the previous ones is that the first line -of `fn fill_vec` that had `let mut vec = vec;` is no longer there. You can, -instead of adding that line back, add `mut` in one place that will change -an existing binding to be a mutable binding instead of an immutable one :)""" +与之前不同:`fn fill_vec` 第一行的 `let mut vec = vec;` 现在已经不存在了。 +你可以在某个地方添加 `mut` 以使现有的不可变绑定变得可变,而非把不同的那一行加回去 :)""" [[exercises]] name = "move_semantics4" path = "exercises/move_semantics/move_semantics4.rs" mode = "compile" hint = """ -Stop reading whenever you feel like you have enough direction :) Or try -doing one step and then fixing the compiler errors that result! -So the end goal is to: - - get rid of the first line in main that creates the new vector - - so then `vec0` doesn't exist, so we can't pass it to `fill_vec` - - we don't want to pass anything to `fill_vec`, so its signature should - reflect that it does not take any arguments - - since we're not creating a new vec in `main` anymore, we need to create - a new vec in `fill_vec`, similarly to the way we did in `main`""" +只要你觉得有确切的目标,就可以停止阅读 :) 或者试着做一个步骤,然后修复编译错误。 +因此,目标有: + - 去掉 main 中创建新 vector 的第一行 + - 所以 `vec0` 并不存在,所以我们不能把它传给 `fill_vec` 。 + - 我们不需要向 `fill_vec` 传递任何东西,所以它的签名应该反映出它不接受任何参数*。 + - 由于我们已不在 `main` 创建 vector ,所以需要在 `fill_vec` 中创建一个新的 vector, + 类似于 `main` 中的做法。 + +译注:练习中 fill_vec 的函数签名已经没有接受参数了,所以估计是在调用的地方""" [[exercises]] name = "move_semantics5" path = "exercises/move_semantics/move_semantics5.rs" mode = "compile" hint = """ -Carefully reason about the range in which each mutable reference is in -vogue. Does it help to update the value of referent (x) immediately after -the mutable reference is taken? Read more about 'Mutable References' -in the book's section References and Borrowing': +仔细推敲每个可变引用的使用范围。 +在获取可变引用后是否能够立即更新引用(x)的值? +在本书的 'References and Borrowing' 部分了解更多关于 'Mutable References' 的信息。 https://doc.rust-lang.org/book/ch04-02-references-and-borrowing.html#mutable-references. """