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: doc/rust.md
+15-3
Original file line number
Diff line number
Diff line change
@@ -2150,9 +2150,21 @@ match x {
2150
2150
~~~~
2151
2151
2152
2152
Records and structures can also be pattern-matched and their fields bound to variables.
2153
-
When matching fields of a record, the fields being matched are specified
2154
-
first, then a placeholder (`_`) represents the remaining fields.
2155
-
2153
+
When matching fields of a record,
2154
+
the fields being matched are specified first,
2155
+
then a placeholder (`_`) represents the remaining fields.
2156
+
2157
+
A pattern that's just a variable binding,
2158
+
like `Nil` in the previous answer,
2159
+
could either refer to an enum variant that's in scope,
2160
+
or bind a new variable.
2161
+
The compiler resolves this ambiguity by forbidding variable bindings that occur in ```match``` patterns from shadowing names of variants that are in scope.
2162
+
For example, wherever ```List``` is in scope,
2163
+
a ```match``` pattern would not be able to bind ```Nil``` as a new name.
2164
+
The compiler interprets a variable pattern `x` as a binding _only_ if there is no variant named `x` in scope.
2165
+
A convention you can use to avoid conflicts is simply to name variants with upper-case letters,
2166
+
and local variables with lower-case letters.
2167
+
2156
2168
~~~~
2157
2169
# type options = {choose: bool, size: ~str};
2158
2170
# type player = {player: ~str, stats: (), options: options};
0 commit comments