From 92800787bbbea2a7272467d77c006f0361230850 Mon Sep 17 00:00:00 2001 From: Xiaochuan Yu Date: Wed, 8 Jun 2022 22:07:56 -0400 Subject: [PATCH 1/4] Make guard examples clearer around _ --- src/flow_control/match/guard.md | 33 +++++++++++++++++++-------------- 1 file changed, 19 insertions(+), 14 deletions(-) diff --git a/src/flow_control/match/guard.md b/src/flow_control/match/guard.md index 9caa65aa00..30d3bda560 100644 --- a/src/flow_control/match/guard.md +++ b/src/flow_control/match/guard.md @@ -3,24 +3,28 @@ A `match` *guard* can be added to filter the arm. ```rust,editable +enum Temperature { + Celsius(i32), + Farenheit(i32), +} + fn main() { - let pair = (2, -2); - // TODO ^ Try different values for `pair` - - println!("Tell me about {:?}", pair); - match pair { - (x, y) if x == y => println!("These are twins"), - // The ^ `if condition` part is a guard - (x, y) if x + y == 0 => println!("Antimatter, kaboom!"), - (x, _) if x % 2 == 1 => println!("The first one is odd"), - _ => println!("No correlation..."), + let temperature = Temperature::C(35); + // ^ TODO try different values for `temperature` + + match temperature { + Temperature::Celsius(t) if t >= 30 => println!("{}C is above 30 Celsius", t), + // The `if condition` part ^ is a guard + Temperature::Celsius(t) => println!("{}C is below 30 Celsius", t), + + Temperature::Farenheit(t) if t >= 86 => println!("{}F is above 86 Farenheit", t), + Temperature::Farenheit(t) => println!("{}F is below 86 Farenheit", t), } } ``` -Note that the compiler does not check arbitrary expressions for whether all -possible conditions have been checked. Therefore, you must use the `_` pattern -at the end. +Note that the compiler won't take guard conditions into account when checking +if all patterns are covered by the match expression. ```rust,editable fn main() { @@ -29,7 +33,7 @@ fn main() { match number { i if i == 0 => println!("Zero"), i if i > 0 => println!("Greater than zero"), - _ => println!("Fell through"), // This should not be possible to reach + // _ => unreachable!("Should never happen."), // uncomment to fix compilation } } ``` @@ -37,3 +41,4 @@ fn main() { ### See also: [Tuples](../../primitives/tuples.md) +[Enums](../../custom_types/enum.md) From f19eb62f1d5c05c2725a8dd5487571e6a6f03892 Mon Sep 17 00:00:00 2001 From: Xiaochuan Yu Date: Wed, 8 Jun 2022 22:23:48 -0400 Subject: [PATCH 2/4] C -> Celsius --- src/flow_control/match/guard.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/flow_control/match/guard.md b/src/flow_control/match/guard.md index 30d3bda560..91965f1be9 100644 --- a/src/flow_control/match/guard.md +++ b/src/flow_control/match/guard.md @@ -9,7 +9,7 @@ enum Temperature { } fn main() { - let temperature = Temperature::C(35); + let temperature = Temperature::Celsius(35); // ^ TODO try different values for `temperature` match temperature { From c22667967b432613648938f14cf7fa1c1185c999 Mon Sep 17 00:00:00 2001 From: Xiaochuan Yu Date: Wed, 8 Jun 2022 22:35:40 -0400 Subject: [PATCH 3/4] add ignore, adjust style --- src/flow_control/match/guard.md | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/flow_control/match/guard.md b/src/flow_control/match/guard.md index 91965f1be9..b20e8a9a13 100644 --- a/src/flow_control/match/guard.md +++ b/src/flow_control/match/guard.md @@ -26,14 +26,15 @@ fn main() { Note that the compiler won't take guard conditions into account when checking if all patterns are covered by the match expression. -```rust,editable +```rust,editable,ignore,mdbook-runnable fn main() { let number: u8 = 4; match number { i if i == 0 => println!("Zero"), i if i > 0 => println!("Greater than zero"), - // _ => unreachable!("Should never happen."), // uncomment to fix compilation + // _ => unreachable!("Should never happen."), + // TODO ^ uncomment to fix compilation } } ``` From 8cbf8092a5cc9f8c951704279c2ce11206525fc6 Mon Sep 17 00:00:00 2001 From: Xiaochuan Yu Date: Wed, 8 Jun 2022 22:52:55 -0400 Subject: [PATCH 4/4] actually above --- src/flow_control/match/guard.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/flow_control/match/guard.md b/src/flow_control/match/guard.md index b20e8a9a13..6dba58f138 100644 --- a/src/flow_control/match/guard.md +++ b/src/flow_control/match/guard.md @@ -13,11 +13,11 @@ fn main() { // ^ TODO try different values for `temperature` match temperature { - Temperature::Celsius(t) if t >= 30 => println!("{}C is above 30 Celsius", t), + Temperature::Celsius(t) if t > 30 => println!("{}C is above 30 Celsius", t), // The `if condition` part ^ is a guard Temperature::Celsius(t) => println!("{}C is below 30 Celsius", t), - Temperature::Farenheit(t) if t >= 86 => println!("{}F is above 86 Farenheit", t), + Temperature::Farenheit(t) if t > 86 => println!("{}F is above 86 Farenheit", t), Temperature::Farenheit(t) => println!("{}F is below 86 Farenheit", t), } }