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

@ -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);

@ -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);

@ -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

@ -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<i32>` as argument
// `fill_vec()` 不再获取 `vec: Vec<i32>` 参数
fn fill_vec() -> Vec<i32> {
let mut vec = vec;

@ -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

@ -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<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!`"""
10 `fill_vec` `vec0' *moved*
`fill_vec` `fill_vec`
13 使 `vec0` `main` `fill_vec`
1. `vec0` `fill_vec`
2. `fill_vec` 便
`Vec<i32>`
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.
"""

Loading…
Cancel
Save