You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: src/patterns.md
+20-5
Original file line number
Diff line number
Diff line change
@@ -597,8 +597,8 @@ Reference patterns are always irrefutable.
597
597
[_OuterAttribute_]: attributes.md
598
598
[TUPLE_INDEX]: tokens.md#tuple-index
599
599
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, enum, and union values that match all criteria defined by its subpatterns.
601
+
They are also used to [destructure](#destructuring) a struct, enum, or union value.
602
602
603
603
On a struct pattern, the fields are referenced by name, index (in the case of tuple structs) or ignored by use of `..`:
604
604
@@ -628,9 +628,21 @@ match t {
628
628
PointTuple {0:10, ..} => (),
629
629
PointTuple {..} => (),
630
630
}
631
+
632
+
# enumMessage {
633
+
# Quit,
634
+
# Move { x:i32, y:i32 },
635
+
# }
636
+
# letm=Message::Quit;
637
+
#
638
+
matchm {
639
+
Message::Quit=> (),
640
+
Message::Move {x:10, y:20} => (),
641
+
Message::Move {..} => (),
642
+
}
631
643
```
632
644
633
-
If `..` is not used, it is required to match all fields:
645
+
If `..` is not used, a struct pattern used to match a struct is required to specify all fields:
634
646
635
647
```rust
636
648
# structStruct {
@@ -649,6 +661,8 @@ match struct_value {
649
661
}
650
662
```
651
663
664
+
A struct pattern used to match a union must specify exactly one field (see [Pattern matching on unions]).
665
+
652
666
The `ref` and/or `mut`_IDENTIFIER_ syntax matches any value and binds it to a variable with the same name as the given field.
653
667
654
668
```rust
@@ -662,7 +676,7 @@ The `ref` and/or `mut` _IDENTIFIER_ syntax matches any value and binds it to a v
662
676
letStruct{a:x, b:y, c:z} =struct_value; // destructure all fields
663
677
```
664
678
665
-
A struct pattern is refutable when one of its subpatterns is refutable.
679
+
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.
666
680
667
681
## Tuple struct patterns
668
682
@@ -676,7 +690,7 @@ A struct pattern is refutable when one of its subpatterns is refutable.
676
690
Tuple struct patterns match tuple struct and enum values that match all criteria defined by its subpatterns.
677
691
They are also used to [destructure](#destructuring) a tuple struct or enum value.
678
692
679
-
A tuple struct pattern is refutable when one of its subpatterns is refutable.
693
+
A tuple 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.
680
694
681
695
## Tuple patterns
682
696
@@ -855,6 +869,7 @@ For example, `x @ A(..) | B(..)` will result in an error that `x` is not bound i
0 commit comments