mirror of
https://github.com/rust-lang/rustlings.git
synced 2026-01-02 16:59:18 +00:00
翻譯
This commit is contained in:
parent
44fc93af91
commit
52434a3438
@ -1,10 +1,10 @@
|
|||||||
# 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. Rust’s enums are most similar to algebraic data types in functional languages, such as F#, OCaml, and Haskell.
|
枚舉是許多語言中的一個特性,但其能力在每種語言中都有所不同。Rust 的枚舉最類似於函數式語言中的代數數據類型,例如 F#、OCaml 和 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.
|
與枚舉結合使用的有用特性是 Rust 的 "模式匹配" 功能,這使得對枚舉的不同值運行不同代碼變得容易。
|
||||||
|
|
||||||
## Further information
|
## 更多資訊
|
||||||
|
|
||||||
- [Enums](https://doc.rust-lang.org/book/ch06-00-enums.html)
|
- [枚舉](https://doc.rust-lang.org/book/ch06-00-enums.html)
|
||||||
- [Pattern syntax](https://doc.rust-lang.org/book/ch18-03-pattern-syntax.html)
|
- [模式語法](https://doc.rust-lang.org/book/ch18-03-pattern-syntax.html)
|
||||||
|
|||||||
@ -1,12 +1,12 @@
|
|||||||
// enums1.rs
|
// enums1.rs
|
||||||
//
|
//
|
||||||
// No hints this time! ;)
|
// 這次沒有提示! ;)
|
||||||
|
|
||||||
// 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: 根據下面的用法定義幾種消息類型
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
|
|||||||
@ -1,13 +1,12 @@
|
|||||||
// enums2.rs
|
// enums2.rs
|
||||||
//
|
//
|
||||||
// Execute `rustlings hint enums2` or use the `hint` watch subcommand for a
|
// 執行 `rustlings hint enums2` 或使用 `hint` watch 子命令來獲取提示。
|
||||||
// hint.
|
|
||||||
|
|
||||||
// I AM NOT DONE
|
// I AM NOT DONE
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
enum Message {
|
enum Message {
|
||||||
// TODO: define the different variants used below
|
// TODO: 定義下面使用的不同變體
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Message {
|
impl Message {
|
||||||
|
|||||||
@ -1,14 +1,13 @@
|
|||||||
// enums3.rs
|
// enums3.rs
|
||||||
//
|
//
|
||||||
// Address all the TODOs to make the tests pass!
|
// 完成所有 TODO 使測試通過!
|
||||||
//
|
//
|
||||||
// Execute `rustlings hint enums3` or use the `hint` watch subcommand for a
|
// 執行 `rustlings hint enums3` 或使用 `hint` watch 子命令來獲取提示。
|
||||||
// hint.
|
|
||||||
|
|
||||||
// I AM NOT DONE
|
// I AM NOT DONE
|
||||||
|
|
||||||
enum Message {
|
enum Message {
|
||||||
// TODO: implement the message variant types based on their usage below
|
// TODO: 根據下面的用法實現消息變體類型
|
||||||
}
|
}
|
||||||
|
|
||||||
struct Point {
|
struct Point {
|
||||||
@ -41,8 +40,8 @@ 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 表達式來處理不同的消息變體
|
||||||
// Remember: When passing a tuple as a function argument, you'll need extra parentheses:
|
// 記住:當作為函數參數傳遞元組時,您需要額外的括號:
|
||||||
// fn function((t, u, p, l, e))
|
// fn function((t, u, p, l, e))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user