Skip to content

Patterns and enums #1460

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Feb 28, 2024
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 16 additions & 4 deletions src/patterns.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can this also include union?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've added that


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

Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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

Expand All @@ -676,7 +688,7 @@ A struct pattern is refutable when one of its subpatterns is refutable.
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

Expand Down