Skip to content

Commit eada9ba

Browse files
committed
Say that Struct patterns can match enum values
1 parent a0b1195 commit eada9ba

File tree

1 file changed

+15
-3
lines changed

1 file changed

+15
-3
lines changed

src/patterns.md

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -597,8 +597,8 @@ Reference patterns are always irrefutable.
597597
[_OuterAttribute_]: attributes.md
598598
[TUPLE_INDEX]: tokens.md#tuple-index
599599

600-
Struct patterns match struct values that match all criteria defined by its subpatterns.
601-
They are also used to [destructure](#destructuring) a struct.
600+
Struct patterns match struct and enum values that match all criteria defined by its subpatterns.
601+
They are also used to [destructure](#destructuring) a struct or enum value.
602602

603603
On a struct pattern, the fields are referenced by name, index (in the case of tuple structs) or ignored by use of `..`:
604604

@@ -628,6 +628,18 @@ match t {
628628
PointTuple {0: 10, ..} => (),
629629
PointTuple {..} => (),
630630
}
631+
632+
# enum Message {
633+
# Quit,
634+
# Move { x: i32, y: i32 },
635+
# }
636+
# let m = Message::Quit;
637+
#
638+
match m {
639+
Message::Quit => (),
640+
Message::Move {x: 10, y: 20} => (),
641+
Message::Move {..} => (),
642+
}
631643
```
632644

633645
If `..` is not used, it is required to match all fields:
@@ -662,7 +674,7 @@ The `ref` and/or `mut` _IDENTIFIER_ syntax matches any value and binds it to a v
662674
let Struct{a: x, b: y, c: z} = struct_value; // destructure all fields
663675
```
664676

665-
A struct pattern is refutable when one of its subpatterns is refutable.
677+
A struct pattern is refutable if the _PathInExpression_ resolves to a constructor of an enum with more than one variant, or one of its subpatterns is refutable.
666678

667679
## Tuple struct patterns
668680

0 commit comments

Comments
 (0)