Grammar summary
This page collects every production from the language chapters. Lexer rules define tokens and separators; syntax rules combine them into types, items, statements, and expressions. Each category has a Show syntax diagram button, and production names link to their diagrams.
Notation explains the symbols. Parser conventions specifies operator precedence and contextual punctuation disambiguation, and provides a syntax coverage table. Typing, lookup, and runtime semantics accompany the productions in their respective chapters.
Lexer summary
Lexer
CHAR → <a 7-bit ASCII source character>
SPACE → U+0020
TAB → U+0009
LF → U+000A
CR → U+000D
IDENTIFIER_OR_KEYWORD → ( ASCII_ALPHA | _ ) ASCII_CONTINUE*
ASCII_ALPHA → [a-z A-Z]
ASCII_CONTINUE → ASCII_ALPHA | DEC_DIGIT | _
NON_KEYWORD_IDENTIFIER → IDENTIFIER_OR_KEYWORDexcept _ and a strict or reserved keyword
IDENTIFIER → NON_KEYWORD_IDENTIFIER
COMMENT → LINE_COMMENT | BLOCK_COMMENT
LINE_COMMENT → // ~LF*
BLOCK_COMMENT → /* ( BLOCK_COMMENT | BLOCK_CHAR )* */
BLOCK_CHAR → CHARnot at the start of /* or */
WHITESPACE → ( SPACE | TAB | LF | CR LF )+
Token →
IDENTIFIER_OR_KEYWORD
| INTEGER_LITERAL
| LIFETIME_TOKEN
| PUNCTUATION
INTEGER_LITERAL →
( DEC_LITERAL | BIN_LITERAL | OCT_LITERAL | HEX_LITERAL ) INTEGER_SUFFIX?
DEC_LITERAL → DEC_DIGIT ( DEC_DIGIT | _ )*
BIN_LITERAL → 0b ( BIN_DIGIT | _ )* BIN_DIGIT ( BIN_DIGIT | _ )*
OCT_LITERAL → 0o ( OCT_DIGIT | _ )* OCT_DIGIT ( OCT_DIGIT | _ )*
HEX_LITERAL → 0x ( HEX_DIGIT | _ )* HEX_DIGIT ( HEX_DIGIT | _ )*
BIN_DIGIT → [0-1]
OCT_DIGIT → [0-7]
DEC_DIGIT → [0-9]
HEX_DIGIT → [0-9 a-f A-F]
INTEGER_SUFFIX → i32 | u32 | isize | usize
LIFETIME_TOKEN → ' IDENTIFIER_OR_KEYWORD
LIFETIME_OR_LABEL → ' NON_KEYWORD_IDENTIFIER
PUNCTUATION →
= | < | <= | == | != | >= | >
| && | || | !
| + | - | * | / | % | ^ | & | | | << | >>
| += | -= | *= | /= | %= | ^= | &= | |= | <<= | >>=
| . | , | ; | : | :: | -> | # | _
| { | } | [ | ] | ( | )
Items summary
Item → UseDeclaration | Function | Struct | ConstantItem | Implementation
UseDeclaration → use UseTree ;
UseTree →
( UsePath? :: )? ( * | { ( UseTree ( , UseTree )* ,? )? } )
| UsePath ( as ( IDENTIFIER | _ ) )?
UsePath → ::? UsePathSegment ( :: UsePathSegment )*
UsePathSegment → IDENTIFIER | self | super | crate
Function →
fn IDENTIFIER GenericParams?
( FunctionParameters? )
FunctionReturnType? WhereClause?
BlockExpression
FunctionParameters →
SelfParam ,?
| ( SelfParam , )? FunctionParam ( , FunctionParam )* ,?
ShorthandSelf → ( & Lifetime? )? mut? self
FunctionParam → IdentifierBinding : Type
FunctionReturnType → -> Type
Struct → OuterAttribute* StructStruct
StructStruct → struct IDENTIFIER GenericParams? WhereClause? { StructFields? }
StructFields → StructField ( , StructField )* ,?
StructField → IDENTIFIER : Type
ConstantItem → const IDENTIFIER : Type = ConstValue ;
InherentImpl → impl GenericParams? Type WhereClause? { AssociatedItem* }
AssociatedItem → ConstantItem | Function
GenericParams → < ( GenericParam ( , GenericParam )* ,? )? >
LifetimeParam → Lifetime ( : LifetimeBounds )?
WhereClause → where ( WhereClauseItem , )* WhereClauseItem?
WhereClauseItem → LifetimeWhereClauseItem | TypeBoundWhereClauseItem
Paths summary
Syntax
PathInExpression → PathExprSegment ( :: PathExprSegment )*
PathExprSegment → PathIdentSegment ( :: GenericArgs )?
PathIdentSegment → IDENTIFIER | self | Self
TypePath → TypePathSegment ( :: TypePathSegment )*
TypePathSegment → PathIdentSegment ( ::? GenericArgs )?
GenericArgs → < GenericArgList? >
GenericArgList → ( GenericArg , )* GenericArg ,?
GenericArg → Lifetime | Type
Types summary
Syntax
Lifetime → LIFETIME_OR_LABEL | 'static | '_
LifetimeBounds → ( Lifetime + )* Lifetime?
TypeParamBounds → TypeParamBound ( + TypeParamBound )* +?
TypeNoBounds →
ParenthesizedType
| TypePath
| UnitType
| ReferenceType
| ArrayType
ParenthesizedType → ( Type )
ArrayType → [ Type ; ConstValue ]
ReferenceType → & Lifetime? mut? TypeNoBounds
UnitType → ( )
Attributes summary
Syntax
OuterAttribute → # [ DeriveAttribute ]
DeriveAttribute → derive ( ( DeriveName ( , DeriveName )* ,? )? )
DeriveName → Copy | Clone | PartialEq | Eq
Statements summary
Syntax
Statement → ; | LetStatement | ExpressionStatement
LetStatement → let IdentifierBinding ( : Type )? = Expression ;
IdentifierBinding → mut? IDENTIFIER
ExpressionStatement →
ExpressionWithoutBlock ;
| ExpressionWithBlock ;?
Expressions summary
Syntax
Expression → ExpressionWithoutBlock | ExpressionWithBlock
ExpressionWithoutBlock →
LiteralExpression
| PathExpression
| OperatorExpression
| GroupedExpression
| ArrayExpression
| IndexExpression
| UnitExpression
| StructExpression
| CallExpression
| MethodCallExpression
| FieldExpression
| ContinueExpression
| BreakExpression
| ReturnExpression
ExpressionWithBlock →
BlockExpression
| LoopExpression
| IfExpression
LiteralExpression → INTEGER_LITERAL | true | false
PathExpression → PathInExpression
BlockExpression → { Statements? }
Statements →
Statement+
| Statement+ ExpressionWithoutBlock
| ExpressionWithoutBlock
OperatorExpression →
BorrowExpression
| DereferenceExpression
| NegationExpression
| ArithmeticOrLogicalExpression
| ComparisonExpression
| LazyBooleanExpression
| TypeCastExpression
| AssignmentExpression
| CompoundAssignmentExpression
NegationExpression →
- Expression
| ! Expression
ArithmeticOrLogicalExpression →
Expression + Expression
| Expression - Expression
| Expression * Expression
| Expression / Expression
| Expression % Expression
| Expression & Expression
| Expression | Expression
| Expression ^ Expression
| Expression << Expression
| Expression >> Expression
ComparisonExpression →
Expression == Expression
| Expression != Expression
| Expression > Expression
| Expression < Expression
| Expression >= Expression
| Expression <= Expression
LazyBooleanExpression →
Expression || Expression
| Expression && Expression
TypeCastExpression → Expression as TypeNoBounds
BorrowExpression →
( & | && ) Expression
| ( & | && ) mut Expression
DereferenceExpression → * Expression
AssignmentExpression → Expression = Expression
CompoundAssignmentExpression →
Expression += Expression
| Expression -= Expression
| Expression *= Expression
| Expression /= Expression
| Expression %= Expression
| Expression &= Expression
| Expression |= Expression
| Expression ^= Expression
| Expression <<= Expression
| Expression >>= Expression
GroupedExpression → ( Expression )
UnitExpression → ( )
ArrayExpression → [ ArrayElements? ]
ArrayElements →
Expression ( , Expression )* ,?
| Expression ; ConstValue
IndexExpression → Expression [ Expression ]
StructExpression → PathInExpression { StructExprFields? }
StructExprFields → StructExprField ( , StructExprField )* ,?
StructExprField → IDENTIFIER : Expression
CallExpression → Expression ( CallParams? )
CallParams → Expression ( , Expression )* ,?
MethodCallExpression → Expression . PathExprSegment ( CallParams? )
FieldExpression → Expression . IDENTIFIER
LoopExpression → InfiniteLoopExpression | PredicateLoopExpression
InfiniteLoopExpression → loop BlockExpression
PredicateLoopExpression → while Conditions BlockExpression
BreakExpression → break Expression?
ContinueExpression → continue
IfExpression →
if Conditions BlockExpression
( else ( BlockExpression | IfExpression ) )?
Conditions → Expressionexcept an unparenthesized StructExpression at the condition/body boundary
ReturnExpression → return Expression?
Constants summary
Syntax
ConstValue → INTEGER_LITERAL | true | false | ConstantPath | - Magnitude | ( ConstValue )
Magnitude → INTEGER_LITERAL | ConstantPath | ( Magnitude )