Skip to content

Commit 151aaf8

Browse files
committed
Add pattern guard docs to language ref
1 parent 9303572 commit 151aaf8

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed

doc/rust.texi

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -676,6 +676,7 @@ The keywords are:
676676
@tab @code{fail}
677677
@tab @code{ret}
678678
@tab @code{be}
679+
@item @code{when}
679680
@end multitable
680681

681682
@node Ref.Lex.Res
@@ -3361,6 +3362,18 @@ let message = alt x @{
33613362
@}
33623363
@end example
33633364

3365+
Finally, alt patterns can accept @emph{pattern guards} to further refine the
3366+
criteria for matching a case. Pattern guards appear after the pattern and
3367+
consist of a bool-typed expression following the @emph{when} keyword. A pattern
3368+
guard may refer to the variables bound within the pattern they follow.
3369+
3370+
@example
3371+
let message = alt maybe_digit @{
3372+
some(x) when x < 10 @{ process_digit(x) @}
3373+
some(x) @{ process_other(x) @}
3374+
@}
3375+
@end example
3376+
33643377

33653378
@node Ref.Expr.Alt.Type
33663379
@subsubsection Ref.Expr.Alt.Type

0 commit comments

Comments
 (0)