@@ -91,10 +91,11 @@ Every binding in each `|` separated pattern must appear in all of the patterns
91
91
in the arm. Every binding of the same name must have the same type, and have
92
92
the same binding mode.
93
93
94
+ ## Match guards
95
+
94
96
Match arms can accept _ match guards_ to further refine the
95
97
criteria for matching a case. Pattern guards appear after the pattern and
96
- consist of a bool-typed expression following the ` if ` keyword. A pattern guard
97
- may refer to the variables bound within the pattern they follow.
98
+ consist of a ` bool ` -typed expression following the ` if ` keyword.
98
99
99
100
When the pattern matches successfully, the pattern guard expression is executed.
100
101
If the expression evaluates to true, the pattern is successfully matched against.
@@ -125,6 +126,15 @@ let message = match maybe_digit {
125
126
> assert_eq! (i . get (), 2 );
126
127
> ```
127
128
129
+ A pattern guard may refer to the variables bound within the pattern they follow .
130
+ When such a variable 's binding mode is by - value ,
131
+ a shared reference is taken to it before evaluating the guard .
132
+ When accessing the variable in the guard by shared reference ,
133
+ the shared reference taken before evaluating the guard is used .
134
+ Only when the guard expression evaluates to true is the by - value variable
135
+ moved , or copied , into the arm 's body . This allows shared borrows to be used
136
+ inside guards without moving out of the scrutinee in case guard fails to match .
137
+
128
138
## Attributes on match arms
129
139
130
140
Outer attributes are allowed on match arms . The only attributes that have
0 commit comments