This commit is contained in:
TimLai666 2024-06-10 18:31:04 +08:00 committed by GitHub
parent 44fc93af91
commit 52434a3438
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 16 additions and 18 deletions

View File

@ -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. Rusts 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)

View File

@ -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() {

View File

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

View File

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