Skip to content

Commit 6864070

Browse files
committed
Document alt record patterns
1 parent 6afecc3 commit 6864070

File tree

1 file changed

+35
-6
lines changed

1 file changed

+35
-6
lines changed

doc/rust.texi

Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3369,12 +3369,12 @@ expression following the @code{alt} when the case block completes.
33693369
@cindex Pattern alt expression
33703370
@cindex Control-flow
33713371

3372-
A pattern @code{alt} expression branches on a @emph{pattern}. The exact form of
3373-
matching that occurs depends on the pattern. Patterns consist of some
3374-
combination of literals, tag constructors, variable binding specifications and
3375-
placeholders (@code{_}). A pattern @code{alt} has a @emph{head expression},
3376-
which is the value to compare to the patterns. The type of the patterns must
3377-
equal the type of the head expression.
3372+
A pattern @code{alt} expression branches on a @emph{pattern}. The exact form
3373+
of matching that occurs depends on the pattern. Patterns consist of some
3374+
combination of literals, destructured tag constructors, records and tuples,
3375+
variable binding specifications and placeholders (@code{_}). A pattern
3376+
@code{alt} has a @emph{head expression}, which is the value to compare to the
3377+
patterns. The type of the patterns must equal the type of the head expression.
33783378

33793379
To execute a pattern @code{alt} expression, first the head expression is
33803380
evaluated, then its value is sequentially compared to the patterns in the arms
@@ -3411,6 +3411,35 @@ variant @code{nil} from a binding to variable @code{nil}. Without the period
34113411
the value of @code{x} would be bound to variable @code{nil} and the compiler
34123412
would issue an error about the final wildcard case being unreachable.
34133413

3414+
Records can also be pattern-matched and their fields bound to variables.
3415+
When matching fields of a record, the fields being matched are specified
3416+
first, then a placeholder (@code{_}) represents the remaining fields.
3417+
3418+
@example
3419+
fn main() @{
3420+
let r = @{
3421+
player: "ralph",
3422+
stats: load_stats(),
3423+
options: @{
3424+
choose: true,
3425+
size: "small"
3426+
@}
3427+
@};
3428+
3429+
alt r @{
3430+
@{options: @{choose: true, _@}, _@} @{
3431+
choose_player(r)
3432+
@}
3433+
@{player: p, options: @{size: "small", _@}, _@} @{
3434+
log p + " is small";
3435+
@}
3436+
_ @{
3437+
next_player();
3438+
@}
3439+
@}
3440+
@}
3441+
@end example
3442+
34143443
Multiple alternative patterns may be joined with the @code{|} operator. A
34153444
range of values may be specified with @code{to}. For example:
34163445

0 commit comments

Comments
 (0)