From 9b02990b8c3f5b9ecbe82d195e23d2522b898c23 Mon Sep 17 00:00:00 2001 From: Mark Saward Date: Sun, 3 Jan 2021 17:43:16 +1100 Subject: [PATCH] Add note for match guards to include catch-all The compiler doesn't check for conditions being exhausted when using arbitrary expressions (#74277). This adds a note with example specifying that you need to cover all remaining conditions with `_`. My only concern is that my example may encourage people to use match guards as I did there, instead of something like: ```rust match number { 0 => println!("Zero"), 1..=u8::MAX => println!("Greater than zero"), } ``` --- src/flow_control/match/guard.md | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/src/flow_control/match/guard.md b/src/flow_control/match/guard.md index 336b7f1233..9caa65aa00 100644 --- a/src/flow_control/match/guard.md +++ b/src/flow_control/match/guard.md @@ -18,6 +18,22 @@ fn main() { } ``` +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. + +```rust,editable +fn main() { + let number: u8 = 4; + + 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 + } +} +``` + ### See also: [Tuples](../../primitives/tuples.md)