Skip to content

Commit faac114

Browse files
Merge pull request #720 from rust-lang/bind-by-move
document `bind_by_move_pattern_guards`
2 parents 9e843ae + 2bb5698 commit faac114

File tree

1 file changed

+13
-2
lines changed

1 file changed

+13
-2
lines changed

src/expressions/match-expr.md

+13-2
Original file line numberDiff line numberDiff line change
@@ -91,10 +91,11 @@ Every binding in each `|` separated pattern must appear in all of the patterns
9191
in the arm. Every binding of the same name must have the same type, and have
9292
the same binding mode.
9393

94+
## Match guards
95+
9496
Match arms can accept _match guards_ to further refine the
9597
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.
9899

99100
When the pattern matches successfully, the pattern guard expression is executed.
100101
If the expression evaluates to true, the pattern is successfully matched against.
@@ -125,6 +126,16 @@ let message = match maybe_digit {
125126
> assert_eq!(i.get(), 2);
126127
> ```
127128
129+
A pattern guard may refer to the variables bound within the pattern they follow.
130+
Before evaluating the guard, a shared reference is taken to the part of the
131+
scrutinee the variable matches on. While evaluating the guard,
132+
this shared reference is then used when accessing the variable.
133+
Only when the guard evaluates to true is the value moved, or copied,
134+
from the scrutinee into the variable. This allows shared borrows to be used
135+
inside guards without moving out of the scrutinee in case guard fails to match.
136+
Moreover, by holding a shared reference while evaluating the guard,
137+
mutation inside guards is also prevented.
138+
128139
## Attributes on match arms
129140
130141
Outer attributes are allowed on match arms. The only attributes that have

0 commit comments

Comments
 (0)