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: src/patterns.md
+26Lines changed: 26 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -257,6 +257,31 @@ Its objective is exclusively to make the matched binding a reference, instead of
257
257
r[patterns.ident.precedent]
258
258
[Path patterns](#path-patterns) take precedence over identifier patterns.
259
259
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].
262
+
>
263
+
> ```rust
264
+
> constEXPECTED_VALUE:u8=42;
265
+
> // ^^^^^^^^^^^^^^ That this constant is in scope affects how the
266
+
> // patterns below are treated.
267
+
>
268
+
> fncheck_value(x:u8) ->Result<u8, u8> {
269
+
> matchx {
270
+
> EXPECTED_VALUE=>Ok(x),
271
+
> // ^^^^^^^^^^^^^^ Parsed as a `PathPattern` that resolves to
272
+
> // the constant `42`.
273
+
> other_value=>Err(x),
274
+
> // ^^^^^^^^^^^ Parsed as an `IdentifierPattern`.
275
+
> }
276
+
> }
277
+
>
278
+
> // If `EXPECTED_VALUE` were treated as an `IdentifierPattern` above,
279
+
> // that pattern would always match, making the function always return
280
+
> // `Ok(_) regardless of the input.
281
+
> assert_eq!(check_value(42), Ok(42));
282
+
> assert_eq!(check_value(43), Err(43));
283
+
> ```
284
+
260
285
r[patterns.ident.constraint]
261
286
Itisanerrorif `ref` or `refmut` isspecifiedandtheidentifiershadowsaconstant.
262
287
@@ -1085,6 +1110,7 @@ For example, `x @ A(..) | B(..)` will result in an error that `x` is not bound i
0 commit comments