diff --git a/exercises/12_options/README.md b/exercises/12_options/README.md index bdd33749..49d9f952 100644 --- a/exercises/12_options/README.md +++ b/exercises/12_options/README.md @@ -1,21 +1,21 @@ -# Options +# 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: +`Option` 類型表示一個可選的值:每個 `Option` 要麼是 `Some` 並包含一個值,要麼是 `None`,不包含任何值。 +`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 -- Optional struct fields -- Struct fields that can be loaned or "taken" -- Optional function arguments -- Nullable pointers -- Swapping things out of difficult situations +- 初始值 +- 函數在未定義所有輸入範圍時的返回值(部分函數) +- 用於報告簡單錯誤的返回值,其中 `None` 表示錯誤 +- 可選的結構體字段 +- 可以被借用或“取走”的結構體字段 +- 可選的函數參數 +- 可空指針 +- 將複雜的事情變簡單 -## Further Information +## 更多資訊 -- [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 Enum Documentation](https://doc.rust-lang.org/std/option/enum.Option.html) +- [Option 枚舉格式](https://doc.rust-lang.org/stable/book/ch10-01-syntax.html#in-enum-definitions) +- [Option 模組文檔](https://doc.rust-lang.org/std/option/) +- [Option 枚舉文檔](https://doc.rust-lang.org/std/option/enum.Option.html) - [if let](https://doc.rust-lang.org/rust-by-example/flow_control/if_let.html) - [while let](https://doc.rust-lang.org/rust-by-example/flow_control/while_let.html) diff --git a/exercises/12_options/options1.rs b/exercises/12_options/options1.rs index 3cbfecd6..3c30a980 100644 --- a/exercises/12_options/options1.rs +++ b/exercises/12_options/options1.rs @@ -1,18 +1,16 @@ // options1.rs // -// Execute `rustlings hint options1` or use the `hint` watch subcommand for a -// hint. +// 執行 `rustlings hint options1` 或使用 `hint` watch 子命令來獲取提示。 // I AM NOT DONE -// This function returns how much icecream there is left in the fridge. -// If it's before 10PM, there's 5 scoops left. At 10PM, someone eats it -// all, so there'll be no more left :( +// 此函數返回冰箱裡剩下多少冰淇淋。 +// 如果是晚上10點之前,還剩下5勺冰淇淋。晚上10點時,有人把它全吃光了, +// 所以不會剩下冰淇淋了 :( fn maybe_icecream(time_of_day: u16) -> Option { - // We use the 24-hour system here, so 10PM is a value of 22 and 12AM is a - // value of 0. The Option output should gracefully handle cases where - // time_of_day > 23. - // TODO: Complete the function body - remember to return an Option! + // 我們這裡使用24小時制,所以晚上10點是22,凌晨12點是0。 + // Option 輸出應該優雅地處理 time_of_day > 23 的情況。 + // TODO: 完成函數主體 - 記得返回一個 Option! ??? } @@ -32,8 +30,7 @@ mod tests { #[test] fn raw_value() { - // TODO: Fix this test. How do you get at the value contained in the - // Option? + // TODO: 修復此測試。您如何獲取 Option 中包含的值? let icecreams = maybe_icecream(12); assert_eq!(icecreams, 5); } diff --git a/exercises/12_options/options2.rs b/exercises/12_options/options2.rs index 4d998e7d..bc100852 100644 --- a/exercises/12_options/options2.rs +++ b/exercises/12_options/options2.rs @@ -1,7 +1,6 @@ // options2.rs // -// Execute `rustlings hint options2` or use the `hint` watch subcommand for a -// hint. +// 執行 `rustlings hint options2` 或使用 `hint` watch 子命令來獲取提示。 // I AM NOT DONE @@ -12,7 +11,7 @@ mod tests { let target = "rustlings"; let optional_target = Some(target); - // TODO: Make this an if let statement whose value is "Some" type + // TODO: 將此轉換為 if let 語句,其值為 "Some" 類型 word = optional_target { assert_eq!(word, target); } @@ -29,9 +28,8 @@ mod tests { let mut cursor = range; - // 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.pop() { assert_eq!(integer, cursor); cursor -= 1; diff --git a/exercises/12_options/options3.rs b/exercises/12_options/options3.rs index 23c15eab..06c53187 100644 --- a/exercises/12_options/options3.rs +++ b/exercises/12_options/options3.rs @@ -1,7 +1,6 @@ // options3.rs // -// Execute `rustlings hint options3` or use the `hint` watch subcommand for a -// hint. +// 執行 `rustlings hint options3` 或使用 `hint` watch 子命令來獲取提示。 // I AM NOT DONE @@ -14,8 +13,8 @@ fn main() { let y: Option = Some(Point { x: 100, y: 200 }); match y { - Some(p) => println!("Co-ordinates are {},{} ", p.x, p.y), - _ => panic!("no match!"), + Some(p) => println!("座標是 {},{} ", p.x, p.y), + _ => panic!("沒有匹配!"), } - y; // Fix without deleting this line. + y; // 修復此處而不刪除此行。 }