From eada9ba8fff77bc70d4746d1cb12973895a854ef Mon Sep 17 00:00:00 2001 From: Matthew Woodcraft Date: Mon, 29 Jan 2024 22:09:14 +0000 Subject: [PATCH 1/3] Say that Struct patterns can match enum values --- src/patterns.md | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/src/patterns.md b/src/patterns.md index 6088973..65c73f5 100644 --- a/src/patterns.md +++ b/src/patterns.md @@ -597,8 +597,8 @@ Reference patterns are always irrefutable. [_OuterAttribute_]: attributes.md [TUPLE_INDEX]: tokens.md#tuple-index -Struct patterns match struct values that match all criteria defined by its subpatterns. -They are also used to [destructure](#destructuring) a struct. +Struct patterns match struct and enum values that match all criteria defined by its subpatterns. +They are also used to [destructure](#destructuring) a struct or enum value. On a struct pattern, the fields are referenced by name, index (in the case of tuple structs) or ignored by use of `..`: @@ -628,6 +628,18 @@ match t { PointTuple {0: 10, ..} => (), PointTuple {..} => (), } + +# enum Message { +# Quit, +# Move { x: i32, y: i32 }, +# } +# let m = Message::Quit; +# +match m { + Message::Quit => (), + Message::Move {x: 10, y: 20} => (), + Message::Move {..} => (), +} ``` 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 let Struct{a: x, b: y, c: z} = struct_value; // destructure all fields ``` -A struct pattern is refutable when one of its subpatterns is refutable. +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. ## Tuple struct patterns From c6049baeaf326d7791e4f76e04c625c61ffc2a7c Mon Sep 17 00:00:00 2001 From: Matthew Woodcraft Date: Mon, 29 Jan 2024 22:09:49 +0000 Subject: [PATCH 2/3] Cover enums in the refutability rule for tuple struct patterns --- src/patterns.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/patterns.md b/src/patterns.md index 65c73f5..c4811a6 100644 --- a/src/patterns.md +++ b/src/patterns.md @@ -688,7 +688,7 @@ A struct pattern is refutable if the _PathInExpression_ resolves to a constructo Tuple struct patterns match tuple struct and enum values that match all criteria defined by its subpatterns. They are also used to [destructure](#destructuring) a tuple struct or enum value. -A tuple struct pattern is refutable when one of its subpatterns is refutable. +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. ## Tuple patterns From 08e5cd4e3c8f6d56d793ac89eb8b9a0841d22f7a Mon Sep 17 00:00:00 2001 From: Matthew Woodcraft Date: Tue, 27 Feb 2024 21:52:50 +0000 Subject: [PATCH 3/3] Say that struct patterns can match union values --- src/patterns.md | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/patterns.md b/src/patterns.md index c4811a6..c92e2dc 100644 --- a/src/patterns.md +++ b/src/patterns.md @@ -597,8 +597,8 @@ Reference patterns are always irrefutable. [_OuterAttribute_]: attributes.md [TUPLE_INDEX]: tokens.md#tuple-index -Struct patterns match struct and enum values that match all criteria defined by its subpatterns. -They are also used to [destructure](#destructuring) a struct or enum value. +Struct patterns match struct, enum, and union values that match all criteria defined by its subpatterns. +They are also used to [destructure](#destructuring) a struct, enum, or union value. On a struct pattern, the fields are referenced by name, index (in the case of tuple structs) or ignored by use of `..`: @@ -642,7 +642,7 @@ match m { } ``` -If `..` is not used, it is required to match all fields: +If `..` is not used, a struct pattern used to match a struct is required to specify all fields: ```rust # struct Struct { @@ -661,6 +661,8 @@ match struct_value { } ``` +A struct pattern used to match a union must specify exactly one field (see [Pattern matching on unions]). + The `ref` and/or `mut` _IDENTIFIER_ syntax matches any value and binds it to a variable with the same name as the given field. ```rust @@ -867,6 +869,7 @@ For example, `x @ A(..) | B(..)` will result in an error that `x` is not bound i [literal expression]: expressions/literal-expr.md [negating]: expressions/operator-expr.md#negation-operators [path]: expressions/path-expr.md +[pattern matching on unions]: items/unions.md#pattern-matching-on-unions [range expressions]: expressions/range-expr.md [structs]: items/structs.md [tuples]: types/tuple.md