Merge pull request #126 from mg-chao/20220104_move_semantics

[rust-exercise] 翻译 move_semantics 内的所有内容
pull/127/head
Sunface 3 years ago committed by GitHub
commit ca135a7c83
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -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) - [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) - [Reference and borrowing](https://doc.rust-lang.org/book/ch04-02-references-and-borrowing.html)

@ -1,5 +1,5 @@
// move_semantics1.rs // move_semantics1.rs
// Make me compile! Execute `rustlings hint move_semantics1` for hints :) // 让我能够编译!执行 `rustex hint move_semantics1` 获取提示 :)
// I AM NOT DONE // I AM NOT DONE
@ -8,7 +8,7 @@ fn main() {
let vec1 = fill_vec(vec0); let vec1 = fill_vec(vec0);
println!("{} has length {} content `{:?}`", "vec1", vec1.len(), vec1); println!("{} has length {} content `{:?}`", "vec1", vec1.len(), vec1);// 译:"{} 长度为 {} 内容是 `{:?}`"
vec1.push(88); vec1.push(88);

@ -1,6 +1,6 @@
// move_semantics2.rs // move_semantics2.rs
// Make me compile without changing line 13! // 在不更改第 13 行的要求下通过编译!
// Execute `rustlings hint move_semantics2` for hints :) // 执行 `rustex hint move_semantics2` 获取提示 :)
// I AM NOT DONE // I AM NOT DONE
@ -9,7 +9,7 @@ fn main() {
let mut vec1 = fill_vec(vec0); let mut vec1 = fill_vec(vec0);
// Do not change the following line! // 不要更改下面那行!
println!("{} has length {} content `{:?}`", "vec0", vec0.len(), vec0); println!("{} has length {} content `{:?}`", "vec0", vec0.len(), vec0);
vec1.push(88); vec1.push(88);

@ -1,7 +1,7 @@
// move_semantics3.rs // 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 // I AM NOT DONE

@ -1,8 +1,7 @@
// move_semantics4.rs // move_semantics4.rs
// Refactor this code so that instead of having `vec0` and creating the vector // 重构这段代码,做到删除 `vec0` ,并在 `fn fill_vec` 而非 `fn main` 中创建 vector
// in `fn main`, we create it within `fn fill_vec` and transfer the // 然后将新创建的 vector 从 `fill_vec` 转移到其调用者。
// freshly created vector from fill_vec to its caller. // 执行 `rustex hint move_semantics4` 获取提示 :)
// Execute `rustlings hint move_semantics4` for hints!
// I AM NOT DONE // I AM NOT DONE
@ -18,7 +17,7 @@ fn main() {
println!("{} has length {} content `{:?}`", "vec1", vec1.len(), vec1); println!("{} has length {} content `{:?}`", "vec1", vec1.len(), vec1);
} }
// `fill_vec()` no longer takes `vec: Vec<i32>` as argument // `fill_vec()` 不再获取 `vec: Vec<i32>` 参数
fn fill_vec() -> Vec<i32> { fn fill_vec() -> Vec<i32> {
let mut vec = vec; let mut vec = vec;

@ -1,7 +1,6 @@
// move_semantics5.rs // move_semantics5.rs
// Make me compile only by reordering the lines in `main()`, but without // 只通过重新排列 `main()` 中的已有行来完成编译,并且不能增加、更改或删除任何行
// adding, changing or removing any of them. // 执行 `rustex hint move_semantics5` 获取提示 :)
// Execute `rustlings hint move_semantics5` for hints :)
// I AM NOT DONE // I AM NOT DONE

@ -152,64 +152,57 @@ name = "move_semantics1"
path = "exercises/move_semantics/move_semantics1.rs" path = "exercises/move_semantics/move_semantics1.rs"
mode = "compile" mode = "compile"
hint = """ hint = """
So you've got the "cannot borrow immutable local variable `vec1` as mutable" error on line 13, 13 "cannot borrow immutable local variable `vec1` as mutable"*
right? The fix for this is going to be adding one keyword, and the addition is NOT on line 13 13
where the error is."""
`vec1` """
[[exercises]] [[exercises]]
name = "move_semantics2" name = "move_semantics2"
path = "exercises/move_semantics/move_semantics2.rs" path = "exercises/move_semantics/move_semantics2.rs"
mode = "compile" mode = "compile"
hint = """ hint = """
So `vec0` is being *moved* into the function `fill_vec` when we call it on 10 `fill_vec` `vec0' *moved*
line 10, which means it gets dropped at the end of `fill_vec`, which means we `fill_vec` `fill_vec`
can't use `vec0` again on line 13 (or anywhere else in `main` after the 13 使 `vec0` `main` `fill_vec`
`fill_vec` call for that matter). We could fix this in a few ways, try them
all! 1. `vec0` `fill_vec`
1. Make another, separate version of the data that's in `vec0` and pass that 2. `fill_vec` 便
to `fill_vec` instead. `Vec<i32>`
2. Make `fill_vec` borrow its argument instead of taking ownership of it, 3. `fill_vec` 西
and then copy the data within the function in order to return an owned `vec1` `println!` """
`Vec<i32>`
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!`"""
[[exercises]] [[exercises]]
name = "move_semantics3" name = "move_semantics3"
path = "exercises/move_semantics/move_semantics3.rs" path = "exercises/move_semantics/move_semantics3.rs"
mode = "compile" mode = "compile"
hint = """ hint = """
The difference between this one and the previous ones is that the first line `fn fill_vec` `let mut vec = vec;`
of `fn fill_vec` that had `let mut vec = vec;` is no longer there. You can, `mut` 使 :)"""
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 :)"""
[[exercises]] [[exercises]]
name = "move_semantics4" name = "move_semantics4"
path = "exercises/move_semantics/move_semantics4.rs" path = "exercises/move_semantics/move_semantics4.rs"
mode = "compile" mode = "compile"
hint = """ 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: - main vector
- get rid of the first line in main that creates the new vector - `vec0` `fill_vec`
- so then `vec0` doesn't exist, so we can't pass it to `fill_vec` - `fill_vec` 西*
- we don't want to pass anything to `fill_vec`, so its signature should - `main` vector `fill_vec` vector
reflect that it does not take any arguments `main`
- 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`""" fill_vec """
[[exercises]] [[exercises]]
name = "move_semantics5" name = "move_semantics5"
path = "exercises/move_semantics/move_semantics5.rs" path = "exercises/move_semantics/move_semantics5.rs"
mode = "compile" mode = "compile"
hint = """ 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 x
the mutable reference is taken? Read more about 'Mutable References' 'References and Borrowing' 'Mutable References'
in the book's section References and Borrowing':
https://doc.rust-lang.org/book/ch04-02-references-and-borrowing.html#mutable-references. https://doc.rust-lang.org/book/ch04-02-references-and-borrowing.html#mutable-references.
""" """

Loading…
Cancel
Save