Skip to content

Commit 1afcfd9

Browse files
authored
Merge pull request #1460 from mattheww/2024-01_patterns
Patterns and enums
2 parents 3417f86 + 08e5cd4 commit 1afcfd9

File tree

1 file changed

+20
-5
lines changed

1 file changed

+20
-5
lines changed

src/patterns.md

+20-5
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, 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.
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,9 +628,21 @@ 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

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:
634646

635647
```rust
636648
# struct Struct {
@@ -649,6 +661,8 @@ match struct_value {
649661
}
650662
```
651663

664+
A struct pattern used to match a union must specify exactly one field (see [Pattern matching on unions]).
665+
652666
The `ref` and/or `mut` _IDENTIFIER_ syntax matches any value and binds it to a variable with the same name as the given field.
653667

654668
```rust
@@ -662,7 +676,7 @@ The `ref` and/or `mut` _IDENTIFIER_ syntax matches any value and binds it to a v
662676
let Struct{a: x, b: y, c: z} = struct_value; // destructure all fields
663677
```
664678

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.
666680

667681
## Tuple struct patterns
668682

@@ -676,7 +690,7 @@ A struct pattern is refutable when one of its subpatterns is refutable.
676690
Tuple struct patterns match tuple struct and enum values that match all criteria defined by its subpatterns.
677691
They are also used to [destructure](#destructuring) a tuple struct or enum value.
678692

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.
680694

681695
## Tuple patterns
682696

@@ -855,6 +869,7 @@ For example, `x @ A(..) | B(..)` will result in an error that `x` is not bound i
855869
[literal expression]: expressions/literal-expr.md
856870
[negating]: expressions/operator-expr.md#negation-operators
857871
[path]: expressions/path-expr.md
872+
[pattern matching on unions]: items/unions.md#pattern-matching-on-unions
858873
[range expressions]: expressions/range-expr.md
859874
[structs]: items/structs.md
860875
[tuples]: types/tuple.md

0 commit comments

Comments
 (0)