Skip to content

Latest commit

 

History

History
1560 lines (1167 loc) · 32.2 KB

lexical-elements.rst

File metadata and controls

1560 lines (1167 loc) · 32.2 KB
.. default-domain:: spec

Lexical Elements

.. informational-section::

:dp:`fls_pqwpf87b84tr` The text of a Rust program consists of [module]s organized into [source file]s. The text of a source file is a sequence of [lexical element]s, each composed of characters, whose rules are presented in this chapter.

Character Set

:dp:`fls_itcth8292ud6` The program text of a Rust program is written using the Unicode character set.

Syntax

:dp:`fls_vfx8byq5zo8t` A character is defined by this document for each cell in the coding space described by Unicode, regardless of whether or not Unicode allocates a character to that cell.

:dp:`fls_pvslhm3chtlb` A :dt:`whitespace character` is one of the following characters:

:dp:`fls_7eifv4ksunu1` A whitespace string is a string that consists of one or more [whitespace character]s.

:dp:`fls_PIDKEm8GiLNL` An :ds:`AsciiCharacter` is any Unicode character in the range 0x00 - 0x7F, both inclusive.

Legality Rules

:dp:`fls_2brw13n9ldgy` The coded representation of a character is tool-defined.

Lexical Elements, Separators, and Punctuation

Syntax

.. syntax::

   LexicalElement ::=
       Comment
     | Identifier
     | Keyword
     | Literal
     | Punctuation

   Punctuation ::=
       Delimiter
     | $$+$$
     | $$-$$
     | $$*$$
     | $$/$$
     | $$%$$
     | $$^$$
     | $$!$$
     | $$&$$
     | $$|$$
     | $$&&$$
     | $$||$$
     | $$<<$$
     | $$>>$$
     | $$+=$$
     | $$-=$$
     | $$*=$$
     | $$/=$$
     | $$%=$$
     | $$^=$$
     | $$&=$$
     | $$|=$$
     | $$<<=$$
     | $$>>=$$
     | $$=$$
     | $$==$$
     | $$!=$$
     | $$>$$
     | $$<$$
     | $$>=$$
     | $$<=$$
     | $$@$$
     | $$_$$
     | $$.$$
     | $$..$$
     | $$...$$
     | $$..=$$
     | $$,$$
     | $$;$$
     | $$:$$
     | $$::$$
     | $$->$$
     | $$=>$$
     | $$#$$
     | $$$$$
     | $$?$$

   Delimiter ::=
       $${$$
     | $$}$$
     | $$[$$
     | $$]$$
     | $$($$
     | $$)$$

Legality Rules

:dp:`fls_d4nvxsvxj537` The text of a source file is a sequence of separate [lexical element]s. The meaning of a program depends only on the particular sequence of [lexical element]s, excluding non-[doc comment]s.

:dp:`fls_a1zylpqha73x` A lexical element is the most basic syntactic element in program text.

:dp:`fls_jy6wifn5r2bu` The text of a source file is divided into [line]s.

:dp:`fls_efdfq9nhpmp5` A line is a sequence of zero or more characters followed by an end of line.

:dp:`fls_go25sisi5fdp` The representation of an end of line is tool-defined.

:dp:`fls_a6t53o8h1vdk` A separator is a character or a string that separates adjacent [lexical element]s. A whitespace string is a separator.

:dp:`fls_8fv63w6f4udl` A :dt:`simple punctuator` is one of the following special characters:

.. syntax::

   $$+$$
   $$-$$
   $$*$$
   $$/$$
   $$%$$
   $$^$$
   $$!$$
   $$&$$
   $$|$$
   $$=$$
   $$>$$
   $$<$$
   $$@$$
   $$_$$
   $$.$$
   $$,$$
   $$;$$
   $$:$$
   $$#$$
   $$$$$
   $$?$$
   $${$$
   $$}$$
   $$[$$
   $$]$$
   $$($$
   $$)$$

:dp:`fls_es0tz1q9cmoo` A :dt:`compound punctuator` is one of the following two or more adjacent special characters:

.. syntax::

   $$&&$$
   $$||$$
   $$<<$$
   $$>>$$
   $$+=$$
   $$-=$$
   $$*=$$
   $$/=$$
   $$%=$$
   $$^=$$
   $$&=$$
   $$|=$$
   $$<<=$$
   $$>>=$$
   $$==$$
   $$!=$$
   $$>=$$
   $$<=$$
   $$..$$
   $$...$$
   $$..=$$
   $$::$$
   $$->$$
   $$=>$$

:dp:`fls_vm86olkeecer` The following [compound punctuator]s are :dt:`[flexible compound punctuator]s`.

.. syntax::

   $$&&$$
   $$||$$
   $$<<$$
   $$>>$$

:dp:`fls_5zxdgxy8tjrq` A flexible compound punctuator may be treated as a single compound punctuator or two adjacent [simple punctuator]s.

:dp:`fls_x89vkq9rwlyt` Each of the special characters listed for single character punctuator is a simple punctuator except if this character is used as a character of a compound punctuator, or a character of a character literal, a comment, a numeric literal, or a string literal.

:dp:`fls_bo3xh8r60ji1` The following names are used when referring to [punctuator]s:

:dp:`fls_sslkjuxjnteu` punctuator name
:dp:`fls_9g1godm0jp0z` + Plus
:dp:`fls_6oith9q0soot` - Minus
:dp:`fls_1dledwdc8fa6` * Star
:dp:`fls_lunw7ucj5ius` / Slash
:dp:`fls_a4oiuhz95uiv` % Percent
:dp:`fls_137x9s6guj6h` ^ Caret
:dp:`fls_y0wdb09cpp1w` ! Not
:dp:`fls_48b7mepiuupz` & And
:dp:`fls_g9h9bsvrsmk1` | Or
:dp:`fls_fxne2xd0zzzo` && And and, lazy boolean and
:dp:`fls_il7zv5x3aw0q` || Or or, lazy boolean or
:dp:`fls_ovcs1qm86ss9` << Shift left
:dp:`fls_wmhlvjm0b0j9` >> Shift right
:dp:`fls_gg42klb2gn9v` += Plus equals
:dp:`fls_icahptg5enj4` -= Minus equals
:dp:`fls_baawlxoi7yd4` *= Star equals
:dp:`fls_m7gt3wfbtm81` /= Slash equals
:dp:`fls_6ewl7gn3sjm2` %= Percent equals
:dp:`fls_nb8q6oq8txv3` ^= Caret equals
:dp:`fls_4nnky9ansr9j` &= And equals
:dp:`fls_h1gvudehmnn9` |= Or equals
:dp:`fls_6yj1c3lh691s` <<= Shift left equals
:dp:`fls_2d3oo9nou9vv` >>= Shift right equals
:dp:`fls_st2vhcy14ud9` = Equals
:dp:`fls_9gdyw71dl25` == Equals equals, logical equality
:dp:`fls_sp8ufz28l9w3` != Not equals
:dp:`fls_7kdr8biodxvz` > Greater than
:dp:`fls_pf92l9bkte2u` < Less than
:dp:`fls_ui40thspgyav` >= Greater than equals, greater than or equal to
:dp:`fls_h33qzachmimc` <= Less than equals, less than or equal to
:dp:`fls_13ud1clgdnyv` @ At
:dp:`fls_7fosi8l2ktz2` _ Underscore
:dp:`fls_9qitp6r75ia6` . Dot
:dp:`fls_g0umao9roi2l` .. Dot dot, exclusive range
:dp:`fls_lamrpdpko48` ... Dot dot dot, ellipsis
:dp:`fls_s4lte9onbmqb` ..= Dot dot equals, inclusive range
:dp:`fls_ywc297y8s0dt` , Comma
:dp:`fls_ijb0fws4gshu` ; Semicolon
:dp:`fls_c25ur4xwbpk0` : Colon
:dp:`fls_9dd9479zzq30` :: Colon colon, path separator
:dp:`fls_kwsu9d3ppv3f` -> Right arrow
:dp:`fls_oh62j9unw4mg` => Fat arrow, Hashrocket
:dp:`fls_g0tltt8qmbum` # Pound
:dp:`fls_ounkw8b8tk4f` $ Dollar sign
:dp:`fls_8ywv8gftsfr1` ? Question mark
:dp:`fls_hsn6zc29ifyx` { Left curly brace
:dp:`fls_o3amqe3ca82d` } Right curly brace
:dp:`fls_lkevfpj7sqd3` [ Left square bracket
:dp:`fls_ff05ge2189z` ] Right square bracket
:dp:`fls_nplkudde6oxf` ( Left parenthesis
:dp:`fls_qwnrklmbz0b` ) Right parenthesis

Identifiers

Syntax

.. syntax::

   Identifier ::=
       NonKeywordIdentifier
     | RawIdentifier

   IdentifierList ::=
       Identifier ($$,$$ Identifier)* $$,$$?

   NonKeywordIdentifier ::=
       PureIdentifier
     | WeakKeyword

   RawIdentifier ::=
       $$r#$$ (PureIdentifier | RawIdentifierKeyword)

   PureIdentifier ::=
       XID_Start XID_Continue*
     | $$_$$ XID_Continue+

   IdentifierOrUnderscore ::=
       Identifier
     | $$_$$

   Renaming ::=
       $$as$$ IdentifierOrUnderscore

:dp:`fls_ls7ymvgd5kfa` A :ds:`RawIdentifierKeyword` is any keyword in category :s:`Keyword`, except crate, self, Self, and super.

:dp:`fls_aqj9aguczgqs` :ds:`XID_Start` and :ds:`XID_Continue` are defined in Unicode Standard Annex #31.

Legality Rules

:dp:`fls_xsdmun5uqy4c` An identifier is a lexical element that refers to a name.

:dp:`fls_ktnf6zkrdy45` A pure identifier is an identifier that does not include [weak keyword]s.

:dp:`fls_jpecw46eh061` A pure identifier shall follow the specification in Unicode Standard Annex #31 for Unicode version 13.0, with the following profile:

:dp:`fls_cs6cbw625np1` Characters 0x200C (zero width non-joiner) and 0x200D (zero width joiner) shall not appear in a pure identifier.

:dp:`fls_irwcldiotei2` A pure identifier shall be restricted to characters in category :s:`AsciiCharacter` in the following contexts:

:dp:`fls_vde7gev5rz4q` [Identifier]s are normalized using Normalization Form C as defined in Unicode Standard Annex #15.

:dp:`fls_j9yh8j8jgdeu` Two [identifier]s are considered the same if they consist of the same sequence of characters after performing normalization.

:dp:`fls_jejt5z8m1yew` [Declarative macro]s and [procedural macro]s shall receive normalized [identifier]s in their input.

Examples

foo
_identifier
r#true
Москва
東京

Literals

Syntax

.. syntax::

   Literal ::=
       BooleanLiteral
     | ByteLiteral
     | ByteStringLiteral
     | CStringLiteral
     | CharacterLiteral
     | NumericLiteral
     | StringLiteral

Legality Rules

:dp:`fls_s76un78zyd0j` A literal is a fixed value in program text.

Byte Literals

Syntax

.. syntax::

   ByteLiteral ::=
       $$b'$$ ByteContent $$'$$

   ByteContent ::=
       ByteCharacter
     | ByteEscape

   ByteEscape ::=
     | $$\0$$
     | $$\"$$
     | $$\'$$
     | $$\t$$
     | $$\n$$
     | $$\r$$
     | $$\\$$
     | $$\x$$ OctalDigit HexadecimalDigit

:dp:`fls_3hpzf12h60u4` A :ds:`ByteCharacter` is any character in category :s:`AsciiCharacter` except characters 0x09 (horizontal tabulation), 0x0A (new line), 0x0D (carriage return), 0x27 (apostrophe), and 0x5C (reverse solidus).

Legality Rules

:dp:`fls_q0qwr83frszx` A byte literal is a literal that denotes a fixed byte value.

:dp:`fls_fggytrv5jvw0` The type of a byte literal is :c:`u8`.

Examples

b'h'
b'\n'
b'\x1B'

Byte String Literals

Syntax

.. syntax::
   ByteStringLiteral ::=
       RawByteStringLiteral
     | SimpleByteStringLiteral

Legality Rules

:dp:`fls_t63zfv5JdUhj` A byte string literal is a literal that consists of multiple :s:`[AsciiCharacter]s`.

:dp:`fls_Xd6LnfzMb7t7` The character sequence 0x0D 0x0A (carriage return, new line) is replaced by 0x0A (new line) inside of a byte string literal.

Simple Byte String Literals

Syntax

.. syntax::

   SimpleByteStringLiteral ::=
       $$b"$$ SimpleByteStringContent* $$"$$

   SimpleByteStringContent ::=
       ByteEscape
     | SimpleByteStringCharacter
     | StringContinuation

:dp:`fls_3dcqhuosqb84` A :ds:`SimpleByteStringCharacter` is any character in category :s:`AsciiCharacter` except characters 0x0D (carriage return), 0x22 (quotation mark), and 0x5C (reverse solidus).

Legality Rules

:dp:`fls_moe3zfx39ox2` A simple byte string literal is a byte string literal that consists of multiple :s:`[AsciiCharacter]s`.

:dp:`fls_vffxb6arj9jf` The type of a simple byte string literal of size N is &'static [u8; N].

Examples

b""
b"a\tb"
b"Multi\
line"
Raw Byte String Literals

Syntax

.. syntax::

   RawByteStringLiteral ::=
       $$br$$ RawByteStringContent

   RawByteStringContent ::=
       NestedRawByteStringContent
     | $$"$$ AsciiCharacter* $$"$$

   NestedRawByteStringContent ::=
       $$#$$ RawByteStringContent $$#$$

Legality Rules

:dp:`fls_yyw7nv651580` A raw byte string literal is a simple byte string literal that does not recognize [escaped character]s.

:dp:`fls_5ybq0euwya42` The type of a raw byte string literal of size N is &'static [u8; N].

Examples

br""
br#""#
br##"left #"# right"##

C String Literals

Syntax

.. syntax::
   CStringLiteral ::=
       RawCStringLiteral
     | SimpleCStringLiteral

Legality Rules

:dp:`fls_VKCW830CzhhN` A c string literal is a literal that consists of multiple characters with an implicit 0x00 byte appended to it.

:dp:`fls_XJprzaEn82Xs` The character sequence 0x0D 0x0A (carriage return, new line) is replaced by 0x0A (new line) inside of a c string literal.

Simple C String Literals

Syntax

.. syntax::

   SimpleCStringLiteral ::=
       $$c"$$ SimpleCStringContent* $$"$$

   SimpleCStringContent ::=
       AsciiEscape
     | SimpleStringCharacter
     | StringContinuation
     | UnicodeEscape

:dp:`fls_fnwQHo7twAom` A simple c string literal is any Unicode character except characters 0x0D (carriage return), 0x22 (quotation mark), 0x5C (reverse solidus) and 0x00 (null byte).

Legality Rules

:dp:`fls_nPI7j0siGP8G` A simple c string literal is a c string literal where the characters are Unicode characters.

:dp:`fls_Ae7LM4Wg0NA7` The type of a simple string literal is :std:`&'static [core::ffi::CStr]`.

Examples

c""
c"cat"
c"\tcol\nrow"
c"bell\x07"
c"\u{B80a}"
c"\
multi\
line\
string"
Raw C String Literals

Syntax

.. syntax::

   RawCStringLiteral ::=
       $$cr$$ RawCStringContent

   RawCStringContent ::=
       NestedRawCStringContent
     | $$"$$ ~[$$\r$$]* $$"$$

   NestedRawCStringContent ::=
       $$#$$ RawCStringContent $$#$$

Legality Rules

:dp:`fls_gLrei65i8Uzq` A raw c string literal is a simple c string literal that does not recognize [escaped character]s.

:dp:`fls_9nJHsg9dCi66` The type of a simple string literal is :std:`&'static [core::ffi::CStr]`.

Examples

cr""
cr#""#
cr##"left #"# right"##

Numeric Literals

Syntax

.. syntax::

   NumericLiteral ::=
       FloatLiteral
     | IntegerLiteral

Legality Rules

:dp:`fls_fqpqnku27v99` A numeric literal is a literal that denotes a number.

Integer Literals

Syntax

.. syntax::

   IntegerLiteral ::=
       IntegerContent IntegerSuffix?

   IntegerContent ::=
       BinaryLiteral
     | DecimalLiteral
     | HexadecimalLiteral
     | OctalLiteral

   BinaryLiteral ::=
       $$0b$$ BinaryDigitOrUnderscore* BinaryDigit BinaryDigitOrUnderscore*

   BinaryDigitOrUnderscore ::=
       BinaryDigit
     | $$_$$

   BinaryDigit ::=
       [$$0$$-$$1$$]

   DecimalLiteral ::=
       DecimalDigit DecimalDigitOrUnderscore*

   DecimalDigitOrUnderscore ::=
       DecimalDigit
     | $$_$$

   DecimalDigit ::=
       [$$0$$-$$9$$]

   HexadecimalLiteral ::=
       $$0x$$ HexadecimalDigitOrUnderscore* HexadecimalDigit HexadecimalDigitOrUnderscore*

   HexadecimalDigitOrUnderscore ::=
       HexadecimalDigit
     | $$_$$

   HexadecimalDigit ::=
       [$$0$$-$$9$$ $$a$$-$$f$$ $$A$$-$$F$$]

   OctalLiteral ::=
       $$0o$$ OctalDigitOrUnderscore* OctalDigit OctalDigitOrUnderscore*

   OctalDigitOrUnderscore ::=
       OctalDigit
     | $$_$$

   OctalDigit ::=
       [$$0$$-$$7$$]

   IntegerSuffix ::=
       SignedIntegerSuffix
     | UnsignedIntegerSuffix

   SignedIntegerSuffix ::=
       $$i8$$
     | $$i16$$
     | $$i32$$
     | $$i64$$
     | $$i128$$
     | $$isize$$

   UnsignedIntegerSuffix ::=
       $$u8$$
     | $$u16$$
     | $$u32$$
     | $$u64$$
     | $$u128$$
     | $$usize$$

Legality Rules

:dp:`fls_vkk2krfn93ry` An integer literal is a numeric literal that denotes a whole number.

:dp:`fls_nxqncu5yq4eu` A binary literal is an integer literal in base 2.

:dp:`fls_rn8xfd66yvst` A decimal literal is an integer literal in base 10.

:dp:`fls_2268lchxkzjp` A hexadecimal literal is an integer literal in base 16.

:dp:`fls_4v7awnutbpoe` An octal literal is an integer literal in base 8.

:dp:`fls_f1e29aj0sqvl` An integer suffix is a component of an integer literal that specifies an explicit integer type.

:dp:`fls_u83mffscqm6` A suffixed integer is an integer literal with an integer suffix.

:dp:`fls_g10nuv14q4jn` An unsuffixed integer is an integer literal without an integer suffix.

:dp:`fls_hpkkvuj1z1ez` The type of a suffixed integer is determined by its integer suffix as follows:

:dp:`fls_50qipwqi3arw` The type of an unsuffixed integer is determined by type inference as follows:

  • :dp:`fls_idzhusp2l908` If an integer type can be uniquely determined from the surrounding program context, then the unsuffixed integer has that type.
  • :dp:`fls_qqrqyc6uhol` If the program context under-constrains the type, then the inferred type is :c:`i32`.
  • :dp:`fls_pexi5jazthq6` If the program context over-constrains the type, then this is considered a static error.

Examples

0b0010_1110_u8
1___2_3
0x4D8a
0o77_52i128
Float Literals

Syntax

.. syntax::

   FloatLiteral ::=
       DecimalLiteral $$.$$
     | DecimalLiteral FloatExponent
     | DecimalLiteral $$.$$ DecimalLiteral FloatExponent?
     | DecimalLiteral ($$.$$ DecimalLiteral)? FloatExponent? FloatSuffix

   FloatExponent ::=
       ExponentLetter ExponentSign? ExponentMagnitude

   ExponentLetter ::=
       $$e$$
     | $$E$$

   ExponentSign ::=
       $$+$$
     | $$-$$

   ExponentMagnitude ::=
       DecimalDigitOrUnderscore* DecimalDigit DecimalDigitOrUnderscore*

   FloatSuffix ::=
       $$f32$$
     | $$f64$$

Legality Rules

:dp:`fls_rzi7oeqokd6e` A float literal is a numeric literal that denotes a fractional number.

:dp:`fls_2ru1zyrykd37` A float suffix is a component of a float literal that specifies an explicit floating-point type.

:dp:`fls_21mhnhplzam7` A suffixed float is a float literal with a float suffix.

:dp:`fls_drqh80k0sfkb` An unsuffixed float is a float literal without a float suffix.

:dp:`fls_cbs7j9pjpusw` The type of a suffixed float is determined by the float suffix as follows:

:dp:`fls_yuhza1muo7o` The type of an unsuffixed float is determined by type inference as follows:

  • :dp:`fls_4sxt1ct7fyen` If a floating-point type can be uniquely determined from the surrounding program context, then the unsuffixed float has that type.
  • :dp:`fls_wa72rssp0jnt` If the program context under-constrains the type, then the inferred type is :c:`f64`.
  • :dp:`fls_x2cw7g8g56f8` If the program context over-constrains the type, then this is considered a static error.

Examples

45.
8E+1_820
3.14e5
8_031.4_e-12f64

Character Literals

Syntax

.. syntax::

   CharacterLiteral ::=
       $$'$$ CharacterContent $$'$$

   CharacterContent ::=
       AsciiEscape
     | CharacterLiteralCharacter
     | UnicodeEscape

   AsciiEscape ::=
     | $$\0$$
     | $$\"$$
     | $$\'$$
     | $$\t$$
     | $$\n$$
     | $$\r$$
     | $$\\$$
     | $$\x$$ OctalDigit HexadecimalDigit

:dp:`fls_j9q9ton57rvl` A :ds:`CharacterLiteralCharacter` is any Unicode character except characters 0x09 (horizontal tabulation), 0x0A (new line), 0x0D (carriage return), 0x27 (apostrophe), and 0x5c (reverse solidus).

:dp:`fls_5v9gx22g5wPm` A :ds:`UnicodeEscape` starts with a \u{ literal, followed by 1 to 6 instances of a :s:`HexadecimalDigit`, inclusive, followed by a } character. It can represent any Unicode codepoint between U+00000 and U+10FFFF, inclusive, except Unicode surrogate codepoints, which exist between the range of U+D800 and U+DFFF, inclusive.

Legality Rules

:dp:`fls_vag2oy4q7d4n` A character literal is a literal that denotes a fixed Unicode character.

:dp:`fls_n8z6p6g564r2` The type of a character literal is :c:`char`.

Examples

'a'
'\t'
'\x1b'
'\u{1F30}'

String Literals

Syntax

.. syntax::

   StringLiteral ::=
       RawStringLiteral
     | SimpleStringLiteral

Legality Rules

:dp:`fls_7fuctvtvdi7x` A string literal is a literal that consists of multiple characters.

:dp:`fls_NyiCpU2tzJlQ` The character sequence 0x0D 0x0A (carriage return, new line) is replaced by 0x0A (new line) inside of a string literal.

Simple String Literals

Syntax

.. syntax::

   SimpleStringLiteral ::=
       $$"$$ SimpleStringContent* $$"$$

   SimpleStringContent ::=
       AsciiEscape
     | SimpleStringCharacter
     | StringContinuation
     | UnicodeEscape

:dp:`fls_1pdzwkt5txfj` A :ds:`SimpleStringCharacter` is any Unicode character except characters 0x0D (carriage return), 0x22 (quotation mark), and 0x5C (reverse solidus).

:dp:`fls_wawtu6j3fiqn` :ds:`StringContinuation` is the character sequence 0x5C 0x0A (reverse solidus, new line).

Legality Rules

:dp:`fls_ycy5ee6orjx` A simple string literal is a string literal where the characters are Unicode characters.

:dp:`fls_6nt5kls21xes` The type of a simple string literal is &'static str.

Examples

""
"cat"
"\tcol\nrow"
"bell\x07"
"\u{B80a}"
"\
multi\
line\
string"
Raw String Literals

Syntax

.. syntax::

   RawStringLiteral ::=
       $$r$$ RawStringContent

   RawStringContent ::=
       NestedRawStringContent
     | $$"$$ ~[$$\r$$]* $$"$$

   NestedRawStringContent ::=
       $$#$$ RawStringContent $$#$$

Legality Rules

:dp:`fls_36suwhbwmq1t` A raw string literal is a simple string literal that does not recognize [escaped character]s.

:dp:`fls_ms43w1towz40` The type of a raw string literal is &'static str.

Examples

r""
r#""#
r##"left #"# right"##

Boolean Literals

Syntax

.. syntax::

   BooleanLiteral ::=
       $$false$$
     | $$true$$

Legality Rules

:dp:`fls_1lll64ftupjd` A boolean literal is a literal that denotes the truth [value]s of logic and Boolean algebra.

:dp:`fls_pgngble3ilyx` The type of a boolean literal is :c:`bool`.

Examples

true

Comments

Syntax

.. syntax::

   Comment ::=
       BlockCommentOrDoc
     | LineCommentOrDoc

   BlockCommentOrDoc ::=
       BlockComment
     | InnerBlockDoc
     | OuterBlockDoc

   LineCommentOrDoc ::=
       LineComment
     | InnerLineDoc
     | OuterLineDoc

   LineComment ::=
       $$//$$
     | $$//$$ (~[$$!$$ $$/$$] | $$//$$) ~[$$\n$$]*

   BlockComment ::=
       $$/*$$ (~[$$!$$ $$*$$] | $$**$$ | BlockCommentOrDoc) (BlockCommentOrDoc | ~[$$*/$$])* $$*/$$
     | $$/**/$$
     | $$/***/$$

   InnerBlockDoc ::=
       $$/*!$$ (BlockCommentOrDoc | ~[$$*/$$ $$\r$$])* $$*/$$

   InnerLineDoc ::=
       $$//!$$ ~[$$\n$$ $$\r$$]*

   OuterBlockDoc ::=
       $$/**$$ (~[$$*$$] | BlockCommentOrDoc) (BlockCommentOrDoc | ~[$$*/$$ $$\r$$])* $$*/$$

   OuterLineDoc ::=
       $$///$$ (~[$$/$$] ~[$$\n$$ $$\r$$]*)?

Legality Rules

:dp:`fls_8obn3dtzpe5f` A comment is a lexical element that acts as an annotation or an explanation in program text.

:dp:`fls_qsbnl11be35s` A block comment is a comment that spans one or more [line]s.

:dp:`fls_nayisy85kyq2` A line comment is a comment that spans exactly one line.

:dp:`fls_k3hj30hjkdhw` An inner block doc is a block comment that applies to an enclosing non-[comment] construct.

:dp:`fls_tspijl68lduc` An inner line doc is a line comment that applies to an enclosing non-[comment] construct.

:dp:`fls_KZp0yiFLTqxb` An inner doc comment is either an inner block doc or an inner line doc.

:dp:`fls_63gzofa9ktic` An outer block doc is a block comment that applies to a subsequent non-[comment] construct.

:dp:`fls_scko7crha0um` An outer line doc is a line comment that applies to a subsequent non-[comment] construct.

:dp:`fls_RYVL9KgaxKvl` An outer doc comment is either an outer block doc or an outer line doc.

:dp:`fls_7n6d3jx61ose` A doc comment is a comment class that includes [inner block doc]s, [inner line doc]s, [outer block doc]s, and [outer line doc]s.

:dp:`fls_6fxcs17n4kw` Character 0x0D (carriage return) shall not appear in a comment.

:dp:`fls_uze7l7cxonk1` [Block comment]s, [inner block doc]s, and [outer block doc]s shall extend one or more [line]s.

:dp:`fls_gy23lwlqw2mc` [Line comment]s, [inner line doc]s, and [outer line doc]s shall extend exactly one line.

:dp:`fls_w7d0skpov1is` [Outer block doc]s and [outer line doc]s shall apply to a subsequent non-[comment] construct.

:dp:`fls_32ncjvj2kn7z` [Inner block doc]s and [inner line doc]s shall apply to an enclosing non-[comment] construct.

:dp:`fls_ok0zvo9vcmzo` [Inner block doc]s and [inner line doc]s are equivalent to attribute :c:`doc` of the form #![doc = content], where content is a string literal form of the comment without the leading //!, /*! amd trailing */ characters.

:dp:`fls_nWtKuPi8Fw6v` [Outer block doc]s and [outer line doc]s are equivalent to attribute :c:`doc` of the form #[doc = content], where content is a string literal form of the comment without the leading ///, /** and trailing */ characters.

Examples

// This is a stand-alone line comment. So is the next line.

////

/* This is a stand-alone
   block comment. */

/*
  /* This is a nested block comment */
*/

/// This outer line comment applies to commented_module.

/** This outer block comment applies to commented_module,
    and is considered documentation. */

pub mod commented_module {

    //! This inner line comment applies to commented_mode.

    /*! This inner block comment applies to commented_module,
        and is considered documentation. */
}

Keywords

Syntax

.. syntax::

   Keyword ::=
       ReservedKeyword
     | StrictKeyword
     | WeakKeyword

Legality Rules

:dp:`fls_dti0uu7rz81w` A keyword is a word in program text that has special meaning.

:dp:`fls_sxg1o4oxql51` [Keyword]s are case sensitive.

Strict Keywords

Syntax

.. syntax::

   StrictKeyword ::=
       $$as$$
     | $$async$$
     | $$await$$
     | $$break$$
     | $$const$$
     | $$continue$$
     | $$crate$$
     | $$dyn$$
     | $$enum$$
     | $$extern$$
     | $$false$$
     | $$fn$$
     | $$for$$
     | $$if$$
     | $$impl$$
     | $$in$$
     | $$let$$
     | $$loop$$
     | $$match$$
     | $$mod$$
     | $$move$$
     | $$mut$$
     | $$pub$$
     | $$ref$$
     | $$return$$
     | $$self$$
     | $$Self$$
     | $$static$$
     | $$struct$$
     | $$super$$
     | $$trait$$
     | $$true$$
     | $$type$$
     | $$unsafe$$
     | $$use$$
     | $$where$$
     | $$while$$

Legality Rules

:dp:`fls_bsh7qsyvox21` A strict keyword is a keyword that always holds its special meaning.

Reserved Keywords

Syntax

.. syntax::

   ReservedKeyword ::=
       $$abstract$$
     | $$become$$
     | $$box$$
     | $$do$$
     | $$final$$
     | $$macro$$
     | $$override$$
     | $$priv$$
     | $$try$$
     | $$typeof$$
     | $$unsized$$
     | $$virtual$$
     | $$yield$$

Legality Rules

:dp:`fls_w4b97ewwnql` A reserved keyword is a keyword that is not yet in use.

Weak Keywords

Syntax

.. syntax::

   WeakKeyword ::=
       $$macro_rules$$
     | $$'static$$
     | $$union$$
     | $$safe$$

Legality Rules

:dp:`fls_bv87t1gvj7bz` A weak keyword is a keyword whose special meaning depends on the context.

:dp:`fls_bl55g03jmayf` Word macro_rules acts as a keyword only when used in the context of a :s:`MacroRulesDefinition`.

:dp:`fls_c354oryv513p` Word 'static acts as a keyword only when used in the context of a :s:`LifetimeIndication`.

:dp:`fls_r9fhuiq1ys1p` Word union acts as a keyword only when used in the context of a :s:`UnionDeclaration`.

:dp:`fls_g0JEluWqBpNc` Word safe acts as a keyword only when used as a qualifier of :s:`Function` or :s:`Static` in the context of a :s:`ExternalBlock`.