Skip to content

Commit 6f47be4

Browse files
committed
Add a 'Reserved prefixes' section to the Tokens page
1 parent f8ba2f1 commit 6f47be4

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

src/tokens.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -659,3 +659,34 @@ them are referred to as "token trees" in [macros]. The three types of brackets
659659
[use declarations]: items/use-declarations.md
660660
[use wildcards]: items/use-declarations.md
661661
[while let]: expressions/loop-expr.md#predicate-pattern-loops
662+
663+
## Reserved prefixes
664+
665+
Some lexical forms known as _reserved prefixes_ are reserved for future use.
666+
667+
Source input which would otherwise be lexically interpreted as a non-raw identifier (or a keyword) which is immediately followed by a `#`, `'`, or `"` character (without intervening whitespace) is identified as a reserved prefix.
668+
669+
Note that raw identifiers, raw string literals, and raw byte string literals may contain a `#` character but are not interpreted as containing a reserved prefix.
670+
671+
Similarly the `r`, `b`, and `br` prefixes used in raw string literals, byte literals, byte string literals, and raw byte string literals are not interpreted as reserved prefixes.
672+
673+
> **Edition Differences**: Starting with the 2021 edition, reserved prefixes are reported as an error by the lexer (in particular, they cannot be passed to macros).
674+
>
675+
> Before the 2021 edition, a reserved prefixes are accepted by the lexer and interpreted as multiple tokens (for example, one token for the identifier or keyword, followed by a `#` token).
676+
>
677+
> Examples accepted in all editions:
678+
> ```rust
679+
> macro_rules! lexes {($($_:tt)*) => {}}
680+
> lexes!{a #foo}
681+
> lexes!{continue 'foo}
682+
> lexes!{match "..." {}}
683+
> lexes!{r#let#foo} // three tokens: r#let # foo
684+
> ```
685+
>
686+
> Examples accepted before the 2021 edition but rejected later:
687+
> ```rust,edition2018
688+
> macro_rules! lexes {($($_:tt)*) => {}}
689+
> lexes!{a#foo}
690+
> lexes!{continue'foo}
691+
> lexes!{match"..." {}}
692+
> ```

0 commit comments

Comments
 (0)