update: translate option

pull/267/head
mg-chao 3 years ago
parent fa016d8b34
commit 68d5310df8

@ -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) - [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) - [Bounds](https://doc.rust-lang.org/rust-by-example/generics/bounds.html)

@ -1,17 +1,33 @@
# Option # Option
Type Option represents an optional value: every Option is either Some and contains a value, or None, and does not. Option 类型代表可选的值:每个 Option 要么是 Some ,包含一个值;要么是 None ,表示空值。
Option types are very common in Rust code, as they have a number of uses: Option 在 Rust 代码中十分常见,因为它有许多用途:
- 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 - 返回 None 作为简单错误的返回值
- Optional struct fields - 可选的结构字段
- Struct fields that can be loaned or "taken" - 可以借用或 "取走" 的结构字段(的值)
- Optional function arguments - 可选的函数参数
- Nullable pointers - 空指针
- Swapping things out of difficult situations - 在某些情况下交换值*
## 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 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/) - [Option Module Documentation](https://doc.rust-lang.org/std/option/)

@ -1,9 +1,9 @@
// option1.rs // option1.rs
// Make me compile! Execute `rustlings hint option1` for hints // 让我通过编译!执行 `rustlings hint option1` 获取提示!
// I AM NOT DONE // I AM NOT DONE
// you can modify anything EXCEPT for this function's sig // 你可以自由修改代码,但这个函数签名除外。
fn print_number(maybe_number: Option<u16>) { fn print_number(maybe_number: Option<u16>) {
println!("printing: {}", maybe_number.unwrap()); println!("printing: {}", maybe_number.unwrap());
} }

@ -1,11 +1,11 @@
// option2.rs // option2.rs
// Make me compile! Execute `rustlings hint option2` for hints // 让我通过编译!执行 `rustlings hint option2` 获取提示!
// I AM NOT DONE // I AM NOT DONE
fn main() { fn main() {
let optional_word = Some(String::from("rustlings")); 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 { word = optional_word {
println!("The word is: {}", word); println!("The word is: {}", word);
} else { } else {
@ -17,8 +17,8 @@ fn main() {
optional_integers_vec.push(Some(x)); optional_integers_vec.push(Some(x));
} }
// TODO: make this a while let statement - remember that vector.pop also adds another layer of Option<T> // TODO:改成 while let 语句——记住vector.pop 的返回类型为 Option<T>。
// You can stack `Option<T>`'s into while let and if let // 你可以多次层叠地对 `Option<T>` 使用 while let 或 if let
integer = optional_integers_vec.pop() { integer = optional_integers_vec.pop() {
println!("current value: {}", integer); println!("current value: {}", integer);
} }

@ -1,5 +1,5 @@
// option3.rs // option3.rs
// Make me compile! Execute `rustlings hint option3` for hints // 让我通过编译!执行 `rustlings hint option3` 获取提示
// I AM NOT DONE // I AM NOT DONE
@ -15,5 +15,5 @@ fn main() {
Some(p) => println!("Co-ordinates are {},{} ", p.x, p.y), Some(p) => println!("Co-ordinates are {},{} ", p.x, p.y),
_ => println!("no match"), _ => println!("no match"),
} }
y; // Fix without deleting this line. y; // 无需删除这行就可以解决。
} }

@ -549,15 +549,15 @@ name = "option1"
path = "exercises/option/option1.rs" path = "exercises/option/option1.rs"
mode = "compile" mode = "compile"
hint = """ hint = """
Hint 1: Check out some functions of Option: 1 Option
is_some is_some
is_none is_none
unwrap 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]] [[exercises]]
@ -565,13 +565,13 @@ name = "option2"
path = "exercises/option/option2.rs" path = "exercises/option/option2.rs"
mode = "compile" mode = "compile"
hint = """ 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/if_let.html
https://doc.rust-lang.org/rust-by-example/flow_control/while_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. Options 使 if let while let
For example: Some(Some(variable)) = variable2 : Some(Some(variable)) = variable2
Also see Option::flatten Option::flatten
""" """
[[exercises]] [[exercises]]
@ -579,10 +579,9 @@ name = "option3"
path = "exercises/option/option3.rs" path = "exercises/option/option3.rs"
mode = "compile" mode = "compile"
hint = """ hint = """
The compiler says a partial move happened in the `match` `match`
statement. How can this be avoided? The compiler shows the correction
needed. After making the correction as suggested by the compiler, do https://doc.rust-lang.org/std/keyword.ref.html"""
read: https://doc.rust-lang.org/std/keyword.ref.html"""
# TRAITS # TRAITS

Loading…
Cancel
Save