Merge pull request #139 from mg-chao/20220107_translate_enums

[rust-exercise] 翻译 enums 部分
pull/141/head
Sunface 3 years ago committed by GitHub
commit b344126d19
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -1,10 +1,11 @@
# Enums # 枚举enums
Rust allows you to define types called "enums" which enumerate possible values. Rust 有一种叫做“枚举”的类型,这种类型列举出了某种集合中所有可能的值。
Enums are a feature in many languages, but their capabilities differ in each language. Rusts enums are most similar to algebraic data types in functional languages, such as F#, OCaml, and Haskell. 枚举是许多语言共有的一个功能,但它的作用在每种语言中都有所不同。
Useful in combination with enums is Rust's "pattern matching" facility, which makes it easy to run different code for different values of an enumeration. 如 F#、OCaml 和 Haskell 之类的函数式语言中的代数数据类型algebraic data types和 Rust 的枚举很相似。
Rust 的“模式匹配”功能与枚举结合起非常强大,利用它我们很容易就能针对枚举的不同类型运行不同的代码。
## Further information ## 更多信息
- [Enums](https://doc.rust-lang.org/book/ch06-00-enums.html) - [Enums](https://doc.rust-lang.org/book/ch06-00-enums.html)
- [Pattern syntax](https://doc.rust-lang.org/book/ch18-03-pattern-syntax.html) - [Pattern syntax](https://doc.rust-lang.org/book/ch18-03-pattern-syntax.html)

@ -1,11 +1,11 @@
// enums1.rs // enums1.rs
// Make me compile! Execute `rustlings hint enums1` for hints! // 让我能够编译!执行 `rustex hint enums1` 获取提示 :)
// I AM NOT DONE // I AM NOT DONE
#[derive(Debug)] #[derive(Debug)]
enum Message { enum Message {
// TODO: define a few types of messages as used below // TODO:遵照下面的使用方式来定义几种 Message 的类型
} }
fn main() { fn main() {

@ -1,11 +1,11 @@
// enums2.rs // enums2.rs
// Make me compile! Execute `rustlings hint enums2` for hints! // 让我能够编译!执行 `rustex hint enums2` 获取提示!
// I AM NOT DONE // I AM NOT DONE
#[derive(Debug)] #[derive(Debug)]
enum Message { enum Message {
// TODO: define the different variants used below // TODO:定义下面使用到的多种 Message 类型
} }
impl Message { impl Message {

@ -1,10 +1,10 @@
// enums3.rs // enums3.rs
// Address all the TODOs to make the tests pass! // 解决所有的 TODO ,通过测试!
// I AM NOT DONE // I AM NOT DONE
enum Message { enum Message {
// TODO: implement the message variant types based on their usage below // TODO:根据以下的使用方式,实现 Message 的不同类型
} }
struct Point { struct Point {
@ -36,7 +36,7 @@ impl State {
} }
fn process(&mut self, message: Message) { fn process(&mut self, message: Message) {
// TODO: create a match expression to process the different message variants // TODO:使用 match 表达式来处理不同类型的消息
} }
} }

@ -309,21 +309,22 @@ name = "enums1"
path = "exercises/enums/enums1.rs" path = "exercises/enums/enums1.rs"
mode = "compile" mode = "compile"
hint = """ hint = """
Hint: The declaration of the enumeration type has not been defined yet.""" """
[[exercises]] [[exercises]]
name = "enums2" name = "enums2"
path = "exercises/enums/enums2.rs" path = "exercises/enums/enums2.rs"
mode = "compile" mode = "compile"
hint = """ hint = """
Hint: you can create enumerations that have different variants with different types *
such as no data, anonymous structs, a single string, tuples, ...etc"""
"""
[[exercises]] [[exercises]]
name = "enums3" name = "enums3"
path = "exercises/enums/enums3.rs" path = "exercises/enums/enums3.rs"
mode = "test" mode = "test"
hint = "No hints this time ;)" hint = "这次没有提示 ;)"
# MODULES # MODULES

Loading…
Cancel
Save