Skip to content

Document how the compiler disambiguates variable patterns from variant p... #3859

New issue

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

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

Already on GitHub? # to your account

Closed
wants to merge 1 commit into from
Closed
Changes from all 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
16 changes: 13 additions & 3 deletions doc/rust.md
Original file line number Diff line number Diff line change
Expand Up @@ -2150,9 +2150,19 @@ match x {
~~~~

Records and structures can also be pattern-matched and their fields bound to variables.
When matching fields of a record, the fields being matched are specified
first, then a placeholder (`_`) represents the remaining fields.

When matching fields of a record,
the fields being matched are specified first,
then a placeholder (`_`) represents the remaining fields.

A pattern that's just a variable binding,
like `Nil` in the previous answer,
could either refer to an enum variant that's in scope,
or bind a new variable.
The compiler resolves this ambiguity by forbidding ```match``` patterns to shadow names of variants that are in scope.
For example, wherever ```List``` is in scope,
a ```match``` pattern would not be able to bind ```Nil``` as a new name.
The compiler interprets a variable pattern `x` as a binding _only_ if there is no variant named `x` in scope.

~~~~
# type options = {choose: bool, size: ~str};
# type player = {player: ~str, stats: (), options: options};
Expand Down