Static items StaticItem -> ItemSafety?[^extern-safety] `static` `mut`? IDENTIFIER `:` Type ( `=` Expression )? `;` [^extern-safety]: The safe and unsafe function qualifiers are only allowed semantically within extern blocks. A static item i...
Constant items ConstantItem -> `const` ( IDENTIFIER | `_` ) `:` Type ( `=` Expression )? `;` A constant item is an optionally named constant value which is not associated with a specific memory location in the program. Constants are essenti...
Unions Union -> `union` IDENTIFIER GenericParams? WhereClause? `{` StructFields? `}` A union declaration uses the same syntax as a struct declaration, except with union in place of struct. A union declaration defines the given name in the t...
Enumerations Enumeration -> `enum` IDENTIFIER GenericParams? WhereClause? `{` EnumVariants? `}` EnumVariants -> EnumVariant ( `,` EnumVariant )* `,`? EnumVariant -> OuterAttribute* Visibility? IDENTIFIER ( EnumVariantTuple | EnumVariantStru...
Structs Struct -> StructStruct | TupleStruct StructStruct -> `struct` IDENTIFIER GenericParams? WhereClause? ( `{` StructFields? `}` | `;` ) TupleStruct -> `struct` IDENTIFIER GenericParams? `(` TupleFields? `)` WhereClause? `;` StructField...
Type aliases TypeAlias -> `type` IDENTIFIER GenericParams? ( `:` Bounds )? WhereClause? ( `=` Type WhereClause?)? `;` A type alias defines a new name for an existing type in the type namespace of the module or block where it is located. Typ...
Functions Function -> FunctionQualifiers `fn` IDENTIFIER GenericParams? `(` FunctionParameters? `)` FunctionReturnType? WhereClause? ( BlockExpression | `;` ) FunctionQualifiers -> `const`? `async`?[^async-edition] ItemSafety?[^extern-quali...
Use declarations UseDeclaration -> `use` UseTree `;` UseTree -> (SimplePath? `::`)? `*` | (SimplePath? `::`)? `{` (UseTree ( `,` UseTree )* `,`?)? `}` | SimplePath ( `as` ( IDENTIFIER | `_` ) )? A use declaration creates one or more local n...
Extern crate declarations ExternCrate -> `extern` `crate` CrateRef AsClause? `;` CrateRef -> IDENTIFIER | `self` AsClause -> `as` ( IDENTIFIER | `_` ) An extern crate declaration specifies a dependency on an external crate. The external cra...
Modules Module -> `unsafe`? `mod` IDENTIFIER `;` | `unsafe`? `mod` IDENTIFIER `{` InnerAttribute* Item* `}` A module is a container for zero or more items. A module item is a module, surrounded in braces, named, and prefixed with the keywor...
Items Item -> OuterAttribute* ( VisItem | MacroItem ) VisItem -> Visibility? ( Module | ExternCrate | UseDeclaration | Function | TypeAlias | Struct | Enumeration | Union | ConstantItem | StaticItem | Trait | Implementation | ExternBlock )...
Conditional compilation ConfigurationPredicate -> ConfigurationOption | ConfigurationAll | ConfigurationAny | ConfigurationNot | `true` | `false` ConfigurationOption -> IDENTIFIER ( `=` ( STRING_LITERAL | RAW_STRING_LITERAL ) )? Configurati...
Crates and source files @root Crate -> InnerAttribute* Item* [!NOTE] Although Rust, like any other language, can be implemented by an interpreter as well as a compiler, the only existing implementation is a compiler, and the language has al...
Procedural macros Procedural macros allow creating syntax extensions as execution of a function. Procedural macros come in one of three flavors: • Function-like macros - custom!(...) • Derive macros - #[derive(CustomDerive)] • Attribute mac...
Macros by example MacroRulesDefinition -> `macro_rules` `!` IDENTIFIER MacroRulesDef MacroRulesDef -> `(` MacroRules `)` `;` | `[` MacroRules `]` `;` | `{` MacroRules `}` MacroRules -> MacroRule ( `;` MacroRule )* `;`? MacroRule -> MacroMat...
Macros The functionality and syntax of Rust can be extended with custom definitions called macros. They are given names, and invoked through a consistent syntax: some_extension!(...). There are two ways to define new macros: • Macros by Exa...
Tokens Token -> RESERVED_TOKEN | RAW_IDENTIFIER | CHAR_LITERAL | STRING_LITERAL | RAW_STRING_LITERAL | BYTE_LITERAL | BYTE_STRING_LITERAL | RAW_BYTE_STRING_LITERAL | C_STRING_LITERAL | RAW_C_STRING_LITERAL | FLOAT_LITERAL | INTEGER_LITERAL...
Whitespace WHITESPACE -> U+0009 // Horizontal tab, `'\t'` | U+000A // Line feed, `'\n'` | U+000B // Vertical tab | U+000C // Form feed | U+000D // Carriage return, `'\r'` | U+0020 // Space, `' '` | U+0085 // Next line | U+200E // Left-to-ri...
Comments @root COMMENT -> LINE_COMMENT | INNER_LINE_DOC | OUTER_LINE_DOC | INNER_BLOCK_DOC | OUTER_BLOCK_DOC | BLOCK_COMMENT LINE_COMMENT -> `//` (~[`/` `!` LF] | `//`) ~LF* | `//` EOF | `//` _immediately followed by LF_ BLOCK_COMMENT -> `/...
Identifiers IDENTIFIER_OR_KEYWORD -> ( XID_Start | `_` ) XID_Continue* XID_Start -> <`XID_Start` defined by Unicode> XID_Continue -> <`XID_Continue` defined by Unicode> RAW_IDENTIFIER -> `r#` IDENTIFIER_OR_KEYWORD NON_KEYWORD_IDENTIFIER ->...