15
15
>   ;  ; _ MatchArm_ ` => ` ( [ _ BlockExpression_ ] | [ _ Expression_ ] ) ` , ` <sup >?</sup >
16
16
>
17
17
> _ MatchArm_ :\
18
- >   ;  ; [ _ OuterAttribute_ ] <sup >\* </sup > _ MatchArmPatterns _ _ MatchArmGuard_ <sup >?</sup >
18
+ >   ;  ; [ _ OuterAttribute_ ] <sup >\* </sup > _ Patterns _ _ MatchArmGuard_ <sup >?</sup >
19
19
>
20
- > _ MatchArmPatterns _ :\
20
+ > _ Patterns _ :\
21
21
>   ;  ; ` | ` <sup >?</sup > [ _ Pattern_ ] ( ` | ` [ _ Pattern_ ] )<sup >\* </sup >
22
22
>
23
23
> _ MatchArmGuard_ :\
@@ -62,7 +62,8 @@ match x {
62
62
Variables bound within the pattern are scoped to the match guard and the arm's
63
63
expression. The [ binding mode] (move, copy, or reference) depends on the pattern.
64
64
65
- Multiple match patterns may be joined with the ` | ` operator:
65
+ Multiple match patterns may be joined with the ` | ` operator. Each pattern will be
66
+ tested in left-to-right sequence until a successful match is found.
66
67
67
68
``` rust
68
69
# let x = 9 ;
@@ -75,17 +76,21 @@ let message = match x {
75
76
assert_eq! (message , " a few" );
76
77
```
77
78
78
- Please notice that the ` 2..=9 ` is a [ Range Pattern] , not a [ Range Expression]
79
- and, thus, only those types of ranges supported by range patterns can be used
80
- in match arms.
79
+ > Note: The ` 2..=9 ` is a [ Range Pattern] , not a [ Range Expression] and, thus,
80
+ > only those types of ranges supported by range patterns can be used in match
81
+ > arms.
82
+
83
+ Every binding in each ` | ` separated pattern must appear in all of the patterns
84
+ in the arm. Every binding of the same name must have the same type, and have
85
+ the same binding mode.
81
86
82
87
Match arms can accept _ match guards_ to further refine the
83
88
criteria for matching a case. Pattern guards appear after the pattern and
84
89
consist of a bool-typed expression following the ` if ` keyword. A pattern guard
85
90
may refer to the variables bound within the pattern they follow.
86
91
87
92
When the pattern matches successfully, the pattern guard expression is executed.
88
- If the expression is truthy , the pattern is successfully matched against.
93
+ If the expression evaluates to true , the pattern is successfully matched against.
89
94
Otherwise, the next pattern, including other matches with the ` | ` operator in
90
95
the same arm, is tested.
91
96
0 commit comments