diff --git a/exercise/exercises/generics/README.md b/exercise/exercises/generics/README.md index de46d503..61484d31 100644 --- a/exercise/exercises/generics/README.md +++ b/exercise/exercises/generics/README.md @@ -1,11 +1,10 @@ -# Generics +# 泛型 -Generics is the topic of generalizing types and functionalities to broader cases. -This is extremely useful for reducing code duplication in many ways, but can call for rather involving syntax. -Namely, being generic requires taking great care to specify over which types a generic type is actually considered valid. -The simplest and most common use of generics is for type parameters. +泛型的主旨是把类型和函数泛化到多种情况。 +这在很多方面有助于减少重复代码,但也可能需要为此使用相当多的语法。 +也就是说,使用泛型的话则需要小心谨慎地标明泛型适用于哪些类型。 -## Further information +## 更多信息 - [Generic Data Types](https://doc.rust-lang.org/stable/book/ch10-01-syntax.html) - [Bounds](https://doc.rust-lang.org/rust-by-example/generics/bounds.html) diff --git a/exercise/exercises/option/README.md b/exercise/exercises/option/README.md index a304bb44..2fd44460 100644 --- a/exercise/exercises/option/README.md +++ b/exercise/exercises/option/README.md @@ -1,17 +1,33 @@ # Option -Type Option represents an optional value: every Option is either Some and contains a value, or None, and does not. -Option types are very common in Rust code, as they have a number of uses: -- Initial values -- Return values for functions that are not defined over their entire input range (partial functions) -- Return value for otherwise reporting simple errors, where None is returned on error -- Optional struct fields -- Struct fields that can be loaned or "taken" -- Optional function arguments -- Nullable pointers -- Swapping things out of difficult situations +Option 类型代表可选的值:每个 Option 要么是 Some ,包含一个值;要么是 None ,表示空值。 +Option 在 Rust 代码中十分常见,因为它有许多用途: +- 初始值 +- 输入值不符合定义的情况下作为函数的返回值(部分函数)。 +- 返回 None 作为简单错误的返回值 +- 可选的结构字段 +- 可以借用或 "取走" 的结构字段(的值) +- 可选的函数参数 +- 空指针 +- 在某些情况下交换值* -## Further Information +译注:“在某些情况下交换值”可以假设有个可变数组,现在要通过两个可变引用来交换其中两个元素的值。但 Rust 显然不允许有两个对数组的可变引用,这时候可以用 Option 包装下元素值,比如: + +``` rust +fn main() { + let mut array = vec![Some(1), Some(2)]; + let a = array.get_mut(0).unwrap().take().unwrap(); + let b = array.get_mut(1).unwrap().replace(a); + *array.get_mut(0).unwrap() = b; + println!("{:?}", array);// [Some(2), Some(1)] +} +``` + +嘿嘿,有点强行了。 +[示例参考](https://zulip-archive.rust-lang.org/stream/122651-general/topic/.60Option.60.20.22swapping.20things.20out.20of.20difficult.20situations.22.3F.html) +[关于 Option 的描述来自于](https://doc.rust-lang.org/std/option/) + +## 更多信息 - [Option Enum Format](https://doc.rust-lang.org/stable/book/ch10-01-syntax.html#in-enum-definitions) - [Option Module Documentation](https://doc.rust-lang.org/std/option/) diff --git a/exercise/exercises/option/option1.rs b/exercise/exercises/option/option1.rs index 602ff1a9..c685fd4d 100644 --- a/exercise/exercises/option/option1.rs +++ b/exercise/exercises/option/option1.rs @@ -1,9 +1,9 @@ // option1.rs -// Make me compile! Execute `rustlings hint option1` for hints +// 让我通过编译!执行 `rustlings hint option1` 获取提示! // I AM NOT DONE -// you can modify anything EXCEPT for this function's sig +// 你可以自由修改代码,但这个函数签名除外。 fn print_number(maybe_number: Option) { println!("printing: {}", maybe_number.unwrap()); } diff --git a/exercise/exercises/option/option2.rs b/exercise/exercises/option/option2.rs index c6b83ece..5884f63d 100644 --- a/exercise/exercises/option/option2.rs +++ b/exercise/exercises/option/option2.rs @@ -1,11 +1,11 @@ // option2.rs -// Make me compile! Execute `rustlings hint option2` for hints +// 让我通过编译!执行 `rustlings hint option2` 获取提示! // I AM NOT DONE fn main() { let optional_word = Some(String::from("rustlings")); - // TODO: Make this an if let statement whose value is "Some" type + // TODO:改成适用于值为 "Some" 类型的 if let 语句, word = optional_word { println!("The word is: {}", word); } else { @@ -17,8 +17,8 @@ fn main() { optional_integers_vec.push(Some(x)); } - // TODO: make this a while let statement - remember that vector.pop also adds another layer of Option - // You can stack `Option`'s into while let and if let + // TODO:改成 while let 语句——记住,vector.pop 的返回类型为 Option。 + // 你可以多次层叠地对 `Option` 使用 while let 或 if let integer = optional_integers_vec.pop() { println!("current value: {}", integer); } diff --git a/exercise/exercises/option/option3.rs b/exercise/exercises/option/option3.rs index 045d2acb..9d6ef9a7 100644 --- a/exercise/exercises/option/option3.rs +++ b/exercise/exercises/option/option3.rs @@ -1,5 +1,5 @@ // option3.rs -// Make me compile! Execute `rustlings hint option3` for hints +// 让我通过编译!执行 `rustlings hint option3` 获取提示 // I AM NOT DONE @@ -15,5 +15,5 @@ fn main() { Some(p) => println!("Co-ordinates are {},{} ", p.x, p.y), _ => println!("no match"), } - y; // Fix without deleting this line. + y; // 无需删除这行就可以解决。 } diff --git a/exercise/info.toml b/exercise/info.toml index cf6743cd..87e9d830 100644 --- a/exercise/info.toml +++ b/exercise/info.toml @@ -549,15 +549,15 @@ name = "option1" path = "exercises/option/option1.rs" mode = "compile" hint = """ -Hint 1: Check out some functions of Option: +提示 1:了解 Option 的一些方法: is_some is_none unwrap -and: -pattern matching +和: +模式匹配 -Hint 2: There are no sensible defaults for the value of an Array; the values need to be filled before use. +提示 2:数组没有合适的默认值;在使用前需要添加这些值。 """ [[exercises]] @@ -565,13 +565,13 @@ name = "option2" path = "exercises/option/option2.rs" mode = "compile" hint = """ -check out: +查看: https://doc.rust-lang.org/rust-by-example/flow_control/if_let.html https://doc.rust-lang.org/rust-by-example/flow_control/while_let.html -Remember that Options can be stacked in if let and while let. -For example: Some(Some(variable)) = variable2 -Also see Option::flatten +记住,可以多次层叠地对 Options 使用 if let 或 while let 。 +例如: Some(Some(variable)) = variable2 +也可以参考下 Option::flatten """ [[exercises]] @@ -579,10 +579,9 @@ name = "option3" path = "exercises/option/option3.rs" mode = "compile" hint = """ -The compiler says a partial move happened in the `match` -statement. How can this be avoided? The compiler shows the correction -needed. After making the correction as suggested by the compiler, do -read: https://doc.rust-lang.org/std/keyword.ref.html""" +编译器说 `match` 语句中发生了部分移动。 +怎样避免这种情况?编译器也已给出了所需的修正。 +按照编译器的建议进行修正后,请阅读:https://doc.rust-lang.org/std/keyword.ref.html""" # TRAITS