Skip to content

Commit 21239a9

Browse files
committed
Mention special matching behaviour of _ for macro_rules!
1 parent 4115c9a commit 21239a9

File tree

1 file changed

+7
-0
lines changed

1 file changed

+7
-0
lines changed

src/macros-by-example.md

+7
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,12 @@ the syntax element that matched them. The keyword metavariable `$crate` can be
140140
used to refer to the current crate; see [Hygiene] below. Metavariables can be
141141
transcribed more than once or not at all.
142142

143+
For reasons of backwards compatibility, although `_` is a (keyword) identifier,
144+
it is not matched by the `ident` fragment specifier. Similarly, though `_` [is
145+
also an expression][_UnderscoreExpression_], a standalone underscore is not
146+
matched by the `expr` fragment specifier. However, `_` is matched by the `expr`
147+
fragment specifier when it appears as a subexpression.
148+
143149
> **Edition Differences**: Starting with the 2021 edition, `pat` fragment-specifiers match top-level or-patterns (that is, they accept [_Pattern_]).
144150
>
145151
> Before the 2021 edition, they match exactly the same fragments as `pat_param` (that is, they accept [_PatternNoTopAlt_]).
@@ -506,6 +512,7 @@ For more detail, see the [formal specification].
506512
[_Token_]: tokens.md
507513
[_TypePath_]: paths.md#paths-in-types
508514
[_Type_]: types.md#type-expressions
515+
[_UnderscoreExpression_]: expressions/underscore-expr.md
509516
[_Visibility_]: visibility-and-privacy.md
510517
[formal specification]: macro-ambiguity.md
511518
[token]: tokens.md

0 commit comments

Comments
 (0)