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
Revise note about {Path,Identifier}Pattern parsing
Let's extend the example to demonstrate, comparatively, what is parsed
as an `IdentifierPattern`. Even though this is the common case,
showing this might be helpful to people less familiar with the
grammar. Let's remove `main`, as that's not needed here. And, in the
code, let's highlight specifically what we're commenting on by
commenting below the construct.
Copy file name to clipboardExpand all lines: src/patterns.md
+15-12Lines changed: 15 additions & 12 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -258,25 +258,28 @@ r[patterns.ident.precedent]
258
258
[Path patterns](#path-patterns) take precedence over identifier patterns.
259
259
260
260
> [!NOTE]
261
-
> When a pattern is a single-segment identifier, the grammar is ambiguous whether it means an [IdentifierPattern] or a [PathPattern]. This ambiguity can only be resolved after [name resolution]. In the following example, the pattern is disambiguated to mean a PathPattern to a constant:
261
+
> When a pattern is a single-segment identifier, the grammar is ambiguous whether it means an [IdentifierPattern] or a [PathPattern]. This ambiguity can only be resolved after [name resolution].
262
262
>
263
263
> ```rust
264
-
> constEXPECTED_VALUE:i32=42;
264
+
> constEXPECTED_VALUE:u8=42;
265
+
> // ^^^^^^^^^^^^^^ That this constant is in scope affects how the
266
+
> // match below is parsed.
265
267
>
266
-
> fncheck_value(x:i32) ->bool {
268
+
> fncheck_value(x:u8) ->Result<u8, u8> {
267
269
> matchx {
268
-
> EXPECTED_VALUE=>true, // PathPattern - matches the constant 42
269
-
> _=>false,
270
+
> EXPECTED_VALUE=>Ok(x),
271
+
> // ^^^^^^^^^^^^^^ Parsed as a `PathPattern` the resolves to
272
+
> // the constant `42`.
273
+
> other_value=>Err(x),
274
+
> // ^^^^^^^^^^^ Parsed as an `IdentifierPattern`.
270
275
> }
271
276
> }
272
277
>
273
-
> fnmain() {
274
-
> // If EXPECTED_VALUE were treated as an IdentifierPattern, it would bind
275
-
> // any value to a new variable, making this function always return true
0 commit comments