@@ -3369,12 +3369,12 @@ expression following the @code{alt} when the case block completes.
3369
3369
@cindex Pattern alt expression
3370
3370
@cindex Control-flow
3371
3371
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.
3378
3378
3379
3379
To execute a pattern @code {alt } expression, first the head expression is
3380
3380
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
3411
3411
the value of @code {x } would be bound to variable @code {nil } and the compiler
3412
3412
would issue an error about the final wildcard case being unreachable.
3413
3413
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
+
3414
3443
Multiple alternative patterns may be joined with the @code {| } operator. A
3415
3444
range of values may be specified with @code {to }. For example:
3416
3445
0 commit comments