diff --git a/listings/ch06-enums-and-pattern-matching/listing-06-01/Cargo.toml b/listings/ch06-enums-and-pattern-matching/listing-06-01/Cargo.toml index 04988d1..e959295 100644 --- a/listings/ch06-enums-and-pattern-matching/listing-06-01/Cargo.toml +++ b/listings/ch06-enums-and-pattern-matching/listing-06-01/Cargo.toml @@ -1,7 +1,6 @@ [package] name = "enums" version = "0.1.0" -authors = ["Your Name "] -edition = "2018" +edition = "2021" [dependencies] diff --git a/listings/ch06-enums-and-pattern-matching/listing-06-02/Cargo.toml b/listings/ch06-enums-and-pattern-matching/listing-06-02/Cargo.toml index 04988d1..e959295 100644 --- a/listings/ch06-enums-and-pattern-matching/listing-06-02/Cargo.toml +++ b/listings/ch06-enums-and-pattern-matching/listing-06-02/Cargo.toml @@ -1,7 +1,6 @@ [package] name = "enums" version = "0.1.0" -authors = ["Your Name "] -edition = "2018" +edition = "2021" [dependencies] diff --git a/listings/ch06-enums-and-pattern-matching/listing-06-03/Cargo.toml b/listings/ch06-enums-and-pattern-matching/listing-06-03/Cargo.toml index 04988d1..e959295 100644 --- a/listings/ch06-enums-and-pattern-matching/listing-06-03/Cargo.toml +++ b/listings/ch06-enums-and-pattern-matching/listing-06-03/Cargo.toml @@ -1,7 +1,6 @@ [package] name = "enums" version = "0.1.0" -authors = ["Your Name "] -edition = "2018" +edition = "2021" [dependencies] diff --git a/listings/ch06-enums-and-pattern-matching/listing-06-04/Cargo.toml b/listings/ch06-enums-and-pattern-matching/listing-06-04/Cargo.toml index 04988d1..e959295 100644 --- a/listings/ch06-enums-and-pattern-matching/listing-06-04/Cargo.toml +++ b/listings/ch06-enums-and-pattern-matching/listing-06-04/Cargo.toml @@ -1,7 +1,6 @@ [package] name = "enums" version = "0.1.0" -authors = ["Your Name "] -edition = "2018" +edition = "2021" [dependencies] diff --git a/listings/ch06-enums-and-pattern-matching/listing-06-04/src/main.rs b/listings/ch06-enums-and-pattern-matching/listing-06-04/src/main.rs index 3ba384f..0507fc1 100644 --- a/listings/ch06-enums-and-pattern-matching/listing-06-04/src/main.rs +++ b/listings/ch06-enums-and-pattern-matching/listing-06-04/src/main.rs @@ -1,5 +1,5 @@ // ANCHOR: here -#[derive(Debug)] // so we can inspect the state in a minute +#[derive(Debug)] // 这样可以立刻看到州的名称 enum UsState { Alabama, Alaska, diff --git a/listings/ch06-enums-and-pattern-matching/listing-06-05/Cargo.toml b/listings/ch06-enums-and-pattern-matching/listing-06-05/Cargo.toml index 04988d1..e959295 100644 --- a/listings/ch06-enums-and-pattern-matching/listing-06-05/Cargo.toml +++ b/listings/ch06-enums-and-pattern-matching/listing-06-05/Cargo.toml @@ -1,7 +1,6 @@ [package] name = "enums" version = "0.1.0" -authors = ["Your Name "] -edition = "2018" +edition = "2021" [dependencies] diff --git a/listings/ch06-enums-and-pattern-matching/listing-06-06/Cargo.toml b/listings/ch06-enums-and-pattern-matching/listing-06-06/Cargo.toml index 04988d1..e959295 100644 --- a/listings/ch06-enums-and-pattern-matching/listing-06-06/Cargo.toml +++ b/listings/ch06-enums-and-pattern-matching/listing-06-06/Cargo.toml @@ -1,7 +1,6 @@ [package] name = "enums" version = "0.1.0" -authors = ["Your Name "] -edition = "2018" +edition = "2021" [dependencies] diff --git a/listings/ch06-enums-and-pattern-matching/listing-06-06/src/main.rs b/listings/ch06-enums-and-pattern-matching/listing-06-06/src/main.rs index 222d545..dc2bffb 100644 --- a/listings/ch06-enums-and-pattern-matching/listing-06-06/src/main.rs +++ b/listings/ch06-enums-and-pattern-matching/listing-06-06/src/main.rs @@ -1,8 +1,8 @@ fn main() { // ANCHOR: here - let some_u8_value = Some(0u8); - match some_u8_value { - Some(3) => println!("three"), + let config_max = Some(3u8); + match config_max { + Some(max) => println!("The maximum is configured to be {}", max), _ => (), } // ANCHOR_END: here diff --git a/listings/ch06-enums-and-pattern-matching/no-listing-01-defining-enums/Cargo.toml b/listings/ch06-enums-and-pattern-matching/no-listing-01-defining-enums/Cargo.toml index 04988d1..e959295 100644 --- a/listings/ch06-enums-and-pattern-matching/no-listing-01-defining-enums/Cargo.toml +++ b/listings/ch06-enums-and-pattern-matching/no-listing-01-defining-enums/Cargo.toml @@ -1,7 +1,6 @@ [package] name = "enums" version = "0.1.0" -authors = ["Your Name "] -edition = "2018" +edition = "2021" [dependencies] diff --git a/listings/ch06-enums-and-pattern-matching/no-listing-02-enum-with-data/Cargo.toml b/listings/ch06-enums-and-pattern-matching/no-listing-02-enum-with-data/Cargo.toml index 04988d1..e959295 100644 --- a/listings/ch06-enums-and-pattern-matching/no-listing-02-enum-with-data/Cargo.toml +++ b/listings/ch06-enums-and-pattern-matching/no-listing-02-enum-with-data/Cargo.toml @@ -1,7 +1,6 @@ [package] name = "enums" version = "0.1.0" -authors = ["Your Name "] -edition = "2018" +edition = "2021" [dependencies] diff --git a/listings/ch06-enums-and-pattern-matching/no-listing-03-variants-with-different-data/Cargo.toml b/listings/ch06-enums-and-pattern-matching/no-listing-03-variants-with-different-data/Cargo.toml index 04988d1..e959295 100644 --- a/listings/ch06-enums-and-pattern-matching/no-listing-03-variants-with-different-data/Cargo.toml +++ b/listings/ch06-enums-and-pattern-matching/no-listing-03-variants-with-different-data/Cargo.toml @@ -1,7 +1,6 @@ [package] name = "enums" version = "0.1.0" -authors = ["Your Name "] -edition = "2018" +edition = "2021" [dependencies] diff --git a/listings/ch06-enums-and-pattern-matching/no-listing-04-structs-similar-to-message-enum/Cargo.toml b/listings/ch06-enums-and-pattern-matching/no-listing-04-structs-similar-to-message-enum/Cargo.toml index 04988d1..e959295 100644 --- a/listings/ch06-enums-and-pattern-matching/no-listing-04-structs-similar-to-message-enum/Cargo.toml +++ b/listings/ch06-enums-and-pattern-matching/no-listing-04-structs-similar-to-message-enum/Cargo.toml @@ -1,7 +1,6 @@ [package] name = "enums" version = "0.1.0" -authors = ["Your Name "] -edition = "2018" +edition = "2021" [dependencies] diff --git a/listings/ch06-enums-and-pattern-matching/no-listing-05-methods-on-enums/Cargo.toml b/listings/ch06-enums-and-pattern-matching/no-listing-05-methods-on-enums/Cargo.toml index 04988d1..e959295 100644 --- a/listings/ch06-enums-and-pattern-matching/no-listing-05-methods-on-enums/Cargo.toml +++ b/listings/ch06-enums-and-pattern-matching/no-listing-05-methods-on-enums/Cargo.toml @@ -1,7 +1,6 @@ [package] name = "enums" version = "0.1.0" -authors = ["Your Name "] -edition = "2018" +edition = "2021" [dependencies] diff --git a/listings/ch06-enums-and-pattern-matching/no-listing-06-option-examples/Cargo.toml b/listings/ch06-enums-and-pattern-matching/no-listing-06-option-examples/Cargo.toml index 04988d1..e959295 100644 --- a/listings/ch06-enums-and-pattern-matching/no-listing-06-option-examples/Cargo.toml +++ b/listings/ch06-enums-and-pattern-matching/no-listing-06-option-examples/Cargo.toml @@ -1,7 +1,6 @@ [package] name = "enums" version = "0.1.0" -authors = ["Your Name "] -edition = "2018" +edition = "2021" [dependencies] diff --git a/listings/ch06-enums-and-pattern-matching/no-listing-07-cant-use-option-directly/Cargo.toml b/listings/ch06-enums-and-pattern-matching/no-listing-07-cant-use-option-directly/Cargo.toml index 04988d1..e959295 100644 --- a/listings/ch06-enums-and-pattern-matching/no-listing-07-cant-use-option-directly/Cargo.toml +++ b/listings/ch06-enums-and-pattern-matching/no-listing-07-cant-use-option-directly/Cargo.toml @@ -1,7 +1,6 @@ [package] name = "enums" version = "0.1.0" -authors = ["Your Name "] -edition = "2018" +edition = "2021" [dependencies] diff --git a/listings/ch06-enums-and-pattern-matching/no-listing-07-cant-use-option-directly/output.txt b/listings/ch06-enums-and-pattern-matching/no-listing-07-cant-use-option-directly/output.txt index 856826c..343b9d2 100644 --- a/listings/ch06-enums-and-pattern-matching/no-listing-07-cant-use-option-directly/output.txt +++ b/listings/ch06-enums-and-pattern-matching/no-listing-07-cant-use-option-directly/output.txt @@ -8,9 +8,5 @@ error[E0277]: cannot add `Option` to `i8` | = help: the trait `Add>` is not implemented for `i8` -error: aborting due to previous error - For more information about this error, try `rustc --explain E0277`. -error: could not compile `enums` - -To learn more, run the command again with --verbose. +error: could not compile `enums` due to previous error diff --git a/listings/ch06-enums-and-pattern-matching/no-listing-08-match-arm-multiple-lines/Cargo.toml b/listings/ch06-enums-and-pattern-matching/no-listing-08-match-arm-multiple-lines/Cargo.toml index 04988d1..e959295 100644 --- a/listings/ch06-enums-and-pattern-matching/no-listing-08-match-arm-multiple-lines/Cargo.toml +++ b/listings/ch06-enums-and-pattern-matching/no-listing-08-match-arm-multiple-lines/Cargo.toml @@ -1,7 +1,6 @@ [package] name = "enums" version = "0.1.0" -authors = ["Your Name "] -edition = "2018" +edition = "2021" [dependencies] diff --git a/listings/ch06-enums-and-pattern-matching/no-listing-09-variable-in-pattern/Cargo.toml b/listings/ch06-enums-and-pattern-matching/no-listing-09-variable-in-pattern/Cargo.toml index 04988d1..e959295 100644 --- a/listings/ch06-enums-and-pattern-matching/no-listing-09-variable-in-pattern/Cargo.toml +++ b/listings/ch06-enums-and-pattern-matching/no-listing-09-variable-in-pattern/Cargo.toml @@ -1,7 +1,6 @@ [package] name = "enums" version = "0.1.0" -authors = ["Your Name "] -edition = "2018" +edition = "2021" [dependencies] diff --git a/listings/ch06-enums-and-pattern-matching/no-listing-10-non-exhaustive-match/Cargo.toml b/listings/ch06-enums-and-pattern-matching/no-listing-10-non-exhaustive-match/Cargo.toml index 04988d1..e959295 100644 --- a/listings/ch06-enums-and-pattern-matching/no-listing-10-non-exhaustive-match/Cargo.toml +++ b/listings/ch06-enums-and-pattern-matching/no-listing-10-non-exhaustive-match/Cargo.toml @@ -1,7 +1,6 @@ [package] name = "enums" version = "0.1.0" -authors = ["Your Name "] -edition = "2018" +edition = "2021" [dependencies] diff --git a/listings/ch06-enums-and-pattern-matching/no-listing-10-non-exhaustive-match/output.txt b/listings/ch06-enums-and-pattern-matching/no-listing-10-non-exhaustive-match/output.txt index 0ab76cc..8054e7d 100644 --- a/listings/ch06-enums-and-pattern-matching/no-listing-10-non-exhaustive-match/output.txt +++ b/listings/ch06-enums-and-pattern-matching/no-listing-10-non-exhaustive-match/output.txt @@ -9,9 +9,5 @@ error[E0004]: non-exhaustive patterns: `None` not covered = help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms = note: the matched value is of type `Option` -error: aborting due to previous error - For more information about this error, try `rustc --explain E0004`. -error: could not compile `enums` - -To learn more, run the command again with --verbose. +error: could not compile `enums` due to previous error diff --git a/listings/ch06-enums-and-pattern-matching/no-listing-11-underscore-placeholder/src/main.rs b/listings/ch06-enums-and-pattern-matching/no-listing-11-underscore-placeholder/src/main.rs deleted file mode 100644 index 6fc8ab0..0000000 --- a/listings/ch06-enums-and-pattern-matching/no-listing-11-underscore-placeholder/src/main.rs +++ /dev/null @@ -1,12 +0,0 @@ -fn main() { - // ANCHOR: here - let some_u8_value = 0u8; - match some_u8_value { - 1 => println!("one"), - 3 => println!("three"), - 5 => println!("five"), - 7 => println!("seven"), - _ => (), - } - // ANCHOR_END: here -} diff --git a/listings/ch06-enums-and-pattern-matching/no-listing-12-if-let/Cargo.toml b/listings/ch06-enums-and-pattern-matching/no-listing-12-if-let/Cargo.toml index 04988d1..e959295 100644 --- a/listings/ch06-enums-and-pattern-matching/no-listing-12-if-let/Cargo.toml +++ b/listings/ch06-enums-and-pattern-matching/no-listing-12-if-let/Cargo.toml @@ -1,7 +1,6 @@ [package] name = "enums" version = "0.1.0" -authors = ["Your Name "] -edition = "2018" +edition = "2021" [dependencies] diff --git a/listings/ch06-enums-and-pattern-matching/no-listing-12-if-let/src/main.rs b/listings/ch06-enums-and-pattern-matching/no-listing-12-if-let/src/main.rs index a2bc0e3..735086d 100644 --- a/listings/ch06-enums-and-pattern-matching/no-listing-12-if-let/src/main.rs +++ b/listings/ch06-enums-and-pattern-matching/no-listing-12-if-let/src/main.rs @@ -1,8 +1,8 @@ fn main() { // ANCHOR: here - let some_u8_value = Some(0u8); - if let Some(3) = some_u8_value { - println!("three"); + let config_max = Some(3u8); + if let Some(max) = config_max { + println!("The maximum is configured to be {}", max); } // ANCHOR_END: here } diff --git a/listings/ch06-enums-and-pattern-matching/no-listing-13-count-and-announce-match/Cargo.toml b/listings/ch06-enums-and-pattern-matching/no-listing-13-count-and-announce-match/Cargo.toml index 04988d1..e959295 100644 --- a/listings/ch06-enums-and-pattern-matching/no-listing-13-count-and-announce-match/Cargo.toml +++ b/listings/ch06-enums-and-pattern-matching/no-listing-13-count-and-announce-match/Cargo.toml @@ -1,7 +1,6 @@ [package] name = "enums" version = "0.1.0" -authors = ["Your Name "] -edition = "2018" +edition = "2021" [dependencies] diff --git a/listings/ch06-enums-and-pattern-matching/no-listing-14-count-and-announce-if-let-else/Cargo.toml b/listings/ch06-enums-and-pattern-matching/no-listing-14-count-and-announce-if-let-else/Cargo.toml index 04988d1..e959295 100644 --- a/listings/ch06-enums-and-pattern-matching/no-listing-14-count-and-announce-if-let-else/Cargo.toml +++ b/listings/ch06-enums-and-pattern-matching/no-listing-14-count-and-announce-if-let-else/Cargo.toml @@ -1,7 +1,6 @@ [package] name = "enums" version = "0.1.0" -authors = ["Your Name "] -edition = "2018" +edition = "2021" [dependencies] diff --git a/listings/ch06-enums-and-pattern-matching/no-listing-11-underscore-placeholder/Cargo.lock b/listings/ch06-enums-and-pattern-matching/no-listing-15-binding-catchall/Cargo.lock similarity index 100% rename from listings/ch06-enums-and-pattern-matching/no-listing-11-underscore-placeholder/Cargo.lock rename to listings/ch06-enums-and-pattern-matching/no-listing-15-binding-catchall/Cargo.lock diff --git a/listings/ch06-enums-and-pattern-matching/no-listing-11-underscore-placeholder/Cargo.toml b/listings/ch06-enums-and-pattern-matching/no-listing-15-binding-catchall/Cargo.toml similarity index 50% rename from listings/ch06-enums-and-pattern-matching/no-listing-11-underscore-placeholder/Cargo.toml rename to listings/ch06-enums-and-pattern-matching/no-listing-15-binding-catchall/Cargo.toml index 04988d1..e959295 100644 --- a/listings/ch06-enums-and-pattern-matching/no-listing-11-underscore-placeholder/Cargo.toml +++ b/listings/ch06-enums-and-pattern-matching/no-listing-15-binding-catchall/Cargo.toml @@ -1,7 +1,6 @@ [package] name = "enums" version = "0.1.0" -authors = ["Your Name "] -edition = "2018" +edition = "2021" [dependencies] diff --git a/listings/ch06-enums-and-pattern-matching/no-listing-15-binding-catchall/src/main.rs b/listings/ch06-enums-and-pattern-matching/no-listing-15-binding-catchall/src/main.rs new file mode 100644 index 0000000..6ce0b59 --- /dev/null +++ b/listings/ch06-enums-and-pattern-matching/no-listing-15-binding-catchall/src/main.rs @@ -0,0 +1,14 @@ +fn main() { + // ANCHOR: here + let dice_roll = 9; + match dice_roll { + 3 => add_fancy_hat(), + 7 => remove_fancy_hat(), + other => move_player(other), + } + + fn add_fancy_hat() {} + fn remove_fancy_hat() {} + fn move_player(num_spaces: u8) {} + // ANCHOR_END: here +} diff --git a/listings/ch06-enums-and-pattern-matching/no-listing-16-underscore-catchall/Cargo.lock b/listings/ch06-enums-and-pattern-matching/no-listing-16-underscore-catchall/Cargo.lock new file mode 100644 index 0000000..f62e8ac --- /dev/null +++ b/listings/ch06-enums-and-pattern-matching/no-listing-16-underscore-catchall/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "enums" +version = "0.1.0" + diff --git a/listings/ch06-enums-and-pattern-matching/no-listing-16-underscore-catchall/Cargo.toml b/listings/ch06-enums-and-pattern-matching/no-listing-16-underscore-catchall/Cargo.toml new file mode 100644 index 0000000..e959295 --- /dev/null +++ b/listings/ch06-enums-and-pattern-matching/no-listing-16-underscore-catchall/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "enums" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/listings/ch06-enums-and-pattern-matching/no-listing-16-underscore-catchall/src/main.rs b/listings/ch06-enums-and-pattern-matching/no-listing-16-underscore-catchall/src/main.rs new file mode 100644 index 0000000..586e237 --- /dev/null +++ b/listings/ch06-enums-and-pattern-matching/no-listing-16-underscore-catchall/src/main.rs @@ -0,0 +1,14 @@ +fn main() { + // ANCHOR: here + let dice_roll = 9; + match dice_roll { + 3 => add_fancy_hat(), + 7 => remove_fancy_hat(), + _ => reroll(), + } + + fn add_fancy_hat() {} + fn remove_fancy_hat() {} + fn reroll() {} + // ANCHOR_END: here +} diff --git a/listings/ch06-enums-and-pattern-matching/no-listing-17-underscore-unit/Cargo.lock b/listings/ch06-enums-and-pattern-matching/no-listing-17-underscore-unit/Cargo.lock new file mode 100644 index 0000000..f62e8ac --- /dev/null +++ b/listings/ch06-enums-and-pattern-matching/no-listing-17-underscore-unit/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "enums" +version = "0.1.0" + diff --git a/listings/ch06-enums-and-pattern-matching/no-listing-17-underscore-unit/Cargo.toml b/listings/ch06-enums-and-pattern-matching/no-listing-17-underscore-unit/Cargo.toml new file mode 100644 index 0000000..e959295 --- /dev/null +++ b/listings/ch06-enums-and-pattern-matching/no-listing-17-underscore-unit/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "enums" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/listings/ch06-enums-and-pattern-matching/no-listing-17-underscore-unit/src/main.rs b/listings/ch06-enums-and-pattern-matching/no-listing-17-underscore-unit/src/main.rs new file mode 100644 index 0000000..e791742 --- /dev/null +++ b/listings/ch06-enums-and-pattern-matching/no-listing-17-underscore-unit/src/main.rs @@ -0,0 +1,13 @@ +fn main() { + // ANCHOR: here + let dice_roll = 9; + match dice_roll { + 3 => add_fancy_hat(), + 7 => remove_fancy_hat(), + _ => (), + } + + fn add_fancy_hat() {} + fn remove_fancy_hat() {} + // ANCHOR_END: here +} diff --git a/src/ch06-02-match.md b/src/ch06-02-match.md index f2820e3..34b842e 100644 --- a/src/ch06-02-match.md +++ b/src/ch06-02-match.md @@ -7,21 +7,7 @@ Rust 有一个叫做 `match` 的极为强大的控制流运算符,它允许我 因为刚刚提到了硬币,让我们用它们来作为一个使用 `match` 的例子!我们可以编写一个函数来获取一个未知的硬币,并以一种类似验钞机的方式,确定它是何种硬币并返回它的美分值,如示例 6-3 中所示。 ```rust -enum Coin { - Penny, - Nickel, - Dime, - Quarter, -} - -fn value_in_cents(coin: Coin) -> u8 { - match coin { - Coin::Penny => 1, - Coin::Nickel => 5, - Coin::Dime => 10, - Coin::Quarter => 25, - } -} +{{#rustdoc_include ../listings/ch06-enums-and-pattern-matching/listing-06-03/src/main.rs:here}} ``` 示例 6-3:一个枚举和一个以枚举成员作为模式的 `match` 表达式 @@ -37,24 +23,7 @@ fn value_in_cents(coin: Coin) -> u8 { 如果分支代码较短的话通常不使用大括号,正如示例 6-3 中的每个分支都只是返回一个值。如果想要在分支中运行多行代码,可以使用大括号。例如,如下代码在每次使用`Coin::Penny` 调用时都会打印出 “Lucky penny!”,同时仍然返回代码块最后的值,`1`: ```rust -# enum Coin { -# Penny, -# Nickel, -# Dime, -# Quarter, -# } -# -fn value_in_cents(coin: Coin) -> u8 { - match coin { - Coin::Penny => { - println!("Lucky penny!"); - 1 - }, - Coin::Nickel => 5, - Coin::Dime => 10, - Coin::Quarter => 25, - } -} +{{#rustdoc_include ../listings/ch06-enums-and-pattern-matching/no-listing-08-match-arm-multiple-lines/src/main.rs:here}} ``` ### 绑定值的模式 @@ -64,19 +33,7 @@ fn value_in_cents(coin: Coin) -> u8 { 作为一个例子,让我们修改枚举的一个成员来存放数据。1999 年到 2008 年间,美国在 25 美分的硬币的一侧为 50 个州的每一个都印刷了不同的设计。其他的硬币都没有这种区分州的设计,所以只有这些 25 美分硬币有特殊的价值。可以将这些信息加入我们的 `enum`,通过改变 `Quarter` 成员来包含一个 `State` 值,示例 6-4 中完成了这些修改: ```rust -#[derive(Debug)] // 这样可以立刻看到州的名称 -enum UsState { - Alabama, - Alaska, - // --snip-- -} - -enum Coin { - Penny, - Nickel, - Dime, - Quarter(UsState), -} +{{#rustdoc_include ../listings/ch06-enums-and-pattern-matching/listing-06-04/src/main.rs:here}} ``` 示例 6-4:`Quarter` 成员也存放了一个 `UsState` 值的 `Coin` 枚举 @@ -86,30 +43,7 @@ enum Coin { 在这些代码的匹配表达式中,我们在匹配 `Coin::Quarter` 成员的分支的模式中增加了一个叫做 `state` 的变量。当匹配到 `Coin::Quarter` 时,变量 `state` 将会绑定 25 美分硬币所对应州的值。接着在那个分支的代码中使用 `state`,如下: ```rust -# #[derive(Debug)] -# enum UsState { -# Alabama, -# Alaska, -# } -# -# enum Coin { -# Penny, -# Nickel, -# Dime, -# Quarter(UsState), -# } -# -fn value_in_cents(coin: Coin) -> u8 { - match coin { - Coin::Penny => 1, - Coin::Nickel => 5, - Coin::Dime => 10, - Coin::Quarter(state) => { - println!("State quarter from {:?}!", state); - 25 - }, - } -} +{{#rustdoc_include ../listings/ch06-enums-and-pattern-matching/no-listing-09-variable-in-pattern/src/main.rs:here}} ``` 如果调用 `value_in_cents(Coin::Quarter(UsState::Alaska))`,`coin` 将是 `Coin::Quarter(UsState::Alaska)`。当将值与每个分支相比较时,没有分支会匹配,直到遇到 `Coin::Quarter(state)`。这时,`state` 绑定的将会是值 `UsState::Alaska`。接着就可以在 `println!` 表达式中使用这个绑定了,像这样就可以获取 `Coin` 枚举的 `Quarter` 成员中内部的州的值。 @@ -123,16 +57,7 @@ fn value_in_cents(coin: Coin) -> u8 { 得益于 `match`,编写这个函数非常简单,它将看起来像示例 6-5 中这样: ```rust -fn plus_one(x: Option) -> Option { - match x { - None => None, - Some(i) => Some(i + 1), - } -} - -let five = Some(5); -let six = plus_one(five); -let none = plus_one(None); +{{#rustdoc_include ../listings/ch06-enums-and-pattern-matching/listing-06-05/src/main.rs:here}} ``` 示例 6-5:一个在 `Option` 上使用 `match` 表达式的函数 @@ -142,13 +67,13 @@ let none = plus_one(None); 让我们更仔细地检查 `plus_one` 的第一行操作。当调用 `plus_one(five)` 时,`plus_one` 函数体中的 `x` 将会是值 `Some(5)`。接着将其与每个分支比较。 ```rust,ignore -None => None, +{{#rustdoc_include ../listings/ch06-enums-and-pattern-matching/listing-06-05/src/main.rs:first_arm}} ``` 值 `Some(5)` 并不匹配模式 `None`,所以继续进行下一个分支。 ```rust,ignore -Some(i) => Some(i + 1), +{{#rustdoc_include ../listings/ch06-enums-and-pattern-matching/listing-06-05/src/main.rs:second_arm}} ``` `Some(5)` 与 `Some(i)` 匹配吗?当然匹配!它们是相同的成员。`i` 绑定了 `Some` 中包含的值,所以 `i` 的值是 `5`。接着匹配分支的代码被执行,所以我们将 `i` 的值加一并返回一个含有值 `6` 的新 `Some`。 @@ -156,7 +81,7 @@ Some(i) => Some(i + 1), 接着考虑下示例 6-5 中 `plus_one` 的第二个调用,这里 `x` 是 `None`。我们进入 `match` 并与第一个分支相比较。 ```rust,ignore -None => None, +{{#rustdoc_include ../listings/ch06-enums-and-pattern-matching/listing-06-05/src/main.rs:first_arm}} ``` 匹配上了!这里没有值来加一,所以程序结束并返回 `=>` 右侧的值 `None`,因为第一个分支就匹配到了,其他的分支将不再比较。 @@ -168,21 +93,13 @@ None => None, `match` 还有另一方面需要讨论。考虑一下 `plus_one` 函数的这个版本,它有一个 bug 并不能编译: ```rust,ignore,does_not_compile -fn plus_one(x: Option) -> Option { - match x { - Some(i) => Some(i + 1), - } -} +{{#rustdoc_include ../listings/ch06-enums-and-pattern-matching/no-listing-10-non-exhaustive-match/src/main.rs:here}} ``` 我们没有处理 `None` 的情况,所以这些代码会造成一个 bug。幸运的是,这是一个 Rust 知道如何处理的 bug。如果尝试编译这段代码,会得到这个错误: -```text -error[E0004]: non-exhaustive patterns: `None` not covered - --> - | -6 | match x { - | ^ pattern `None` not covered +```console +{{#include ../listings/ch06-enums-and-pattern-matching/no-listing-10-non-exhaustive-match/output.txt}} ``` Rust 知道我们没有覆盖所有可能的情况甚至知道哪些模式被忘记了!Rust 中的匹配是**穷举式的**(*exhaustive*):必须穷举到最后的可能性来使代码有效。特别的在这个 `Option` 的例子中,Rust 防止我们忘记明确的处理 `None` 的情况,这让我们免于假设拥有一个实际上为空的值,从而使之前提到的价值亿万的错误不可能发生。 @@ -192,16 +109,7 @@ Rust 知道我们没有覆盖所有可能的情况甚至知道哪些模式被忘 让我们看一个例子,我们希望对一些特定的值采取特殊操作,而对其他的值采取默认操作。想象我们正在玩一个游戏,如果你掷出骰子的值为 3,角色不会移动,而是会得到一顶新奇的帽子。如果你掷出了 7,你的角色将失去新奇的帽子。对于其他的数值,你的角色会在棋盘上移动相应的格子。这是一个实现了上述逻辑的 `match`,骰子的结果是硬编码而不是一个随机值,其他的逻辑部分使用了没有函数体的函数来表示,实现它们超出了本例的范围: ```rust -let dice_roll = 9; -match dice_roll { - 3 => add_fancy_hat(), - 7 => remove_fancy_hat(), - other => move_player(other), -} - -fn add_fancy_hat() {} -fn remove_fancy_hat() {} -fn move_player(num_spaces: u8) {} +{{#rustdoc_include ../listings/ch06-enums-and-pattern-matching/no-listing-15-binding-catchall/src/main.rs:here}} ``` +对于前两个分支,匹配模式是字面值 3 和 7,最后一个分支则涵盖了所有其他可能的值,模式是我们命名为 `other` 的一个变量。`other` 分支的代码通过将其传递给 `move_player` 函数来使用这个变量。 @@ -213,34 +121,15 @@ Rust 还提供了一个模式,当我们不想使用通配模式获取的值时 让我们改变游戏规则,当你掷出的值不是 3 或 7 的时候,你必须再次掷出。这种情况下我们不需要使用这个值,所以我们改动代码使用 `_` 来替代变量 `other` : ```rust -let dice_roll = 9; -match dice_roll { - 3 => add_fancy_hat(), - 7 => remove_fancy_hat(), - _ => reroll(), -} - -fn add_fancy_hat() {} -fn remove_fancy_hat() {} -fn reroll() {} +{{#rustdoc_include ../listings/ch06-enums-and-pattern-matching/no-listing-16-underscore-catchall/src/main.rs:here}} ``` 这个例子也满足穷举性要求,因为我们在最后一个分支中明确地忽略了其他的值。我们没有忘记处理任何东西。 让我们再次改变游戏规则,如果你掷出 3 或 7 以外的值,你的回合将无事发生。我们可以使用单元值(在[“元组类型”][tuples]一节中提到的空元组)作为 `_` 分支的代码: -[tuples]: ch03-02-data-types.html#元组类型 - ```rust -let dice_roll = 9; -match dice_roll { - 3 => add_fancy_hat(), - 7 => remove_fancy_hat(), - _ => (), -} - -fn add_fancy_hat() {} -fn remove_fancy_hat() {} +{{#rustdoc_include ../listings/ch06-enums-and-pattern-matching/no-listing-17-underscore-unit/src/main.rs:here}} ``` 在这里,我们明确告诉 Rust 我们不会使用与前面模式不匹配的值,并且这种情况下我们不想运行任何代码。