Grammar
Lexical productions
Keywords
KW_AS :
as
KW_BREAK :break
KW_CONST :const
KW_CONTINUE :continue
KW_CRATE :crate
KW_ELSE :else
KW_ENUM :enum
KW_EXTERN :extern
KW_FALSE :false
KW_FN :fn
KW_FOR :for
KW_IF :if
KW_IMPL :impl
KW_IN :in
KW_LET :let
KW_LOOP :loop
KW_MATCH :match
KW_MOD :mod
KW_MOVE :move
KW_MUT :mut
KW_PUB :pub
KW_REF :ref
KW_RETURN :return
KW_SELFVALUE :self
KW_SELFTYPE :Self
KW_STATIC :static
KW_STRUCT :struct
KW_SUPER :super
KW_TRAIT :trait
KW_TRUE :true
KW_TYPE :type
KW_UNSAFE :unsafe
KW_USE :use
KW_WHERE :where
KW_WHILE :whileKW_ABSTRACT :
abstract
KW_ALIGNOF :alignof
KW_BECOME :become
KW_BOX :box
KW_DO :do
KW_FINAL :final
KW_MACRO :macro
KW_OFFSETOF :offsetof
KW_OVERRIDE :override
KW_PRIV :priv
KW_PROC :proc
KW_PURE :pure
KW_SIZEOF :sizeof
KW_TYPEOF :typeof
KW_UNSIZED :unsized
KW_VIRTUAL :virtual
KW_YIELD :yieldKW_UNION :
union
KW_STATICLIFETIME :'static
Identifiers
IDENTIFIER_OR_KEYWORD :
[a-zA-Z] [a-zA-Z0-9_]*
|_[a-zA-Z0-9_]+IDENTIFIER :
IDENTIFIER_OR_KEYWORD Except a [strict] or [reserved] keyword
Comments
LINE_COMMENT :
//(~[/!] |//) ~\n*
|//BLOCK_COMMENT :
/*(~[*!] |**| BlockCommentOrDoc) (BlockCommentOrDoc | ~*/)**/
|/**/
|/***/INNER_LINE_DOC :
//!~[\nIsolatedCR]*INNER_BLOCK_DOC :
/*!( BlockCommentOrDoc | ~[*/IsolatedCR] )**/OUTER_LINE_DOC :
///(~/~[\nIsolatedCR]*)?OUTER_BLOCK_DOC :
/**(~*| BlockCommentOrDoc ) (BlockCommentOrDoc | ~[*/IsolatedCR])**/BlockCommentOrDoc :
BLOCK_COMMENT
| OUTER_BLOCK_DOC
| INNER_BLOCK_DOCIsolatedCR :
A\rnot followed by a\n
Tokens
CHAR_LITERAL :
'( ~['\\n \r \t] | QUOTE_ESCAPE | ASCII_ESCAPE | UNICODE_ESCAPE )'QUOTE_ESCAPE :
\'|\"ASCII_ESCAPE :
\xOCT_DIGIT HEX_DIGIT
|\n|\r|\t|\\|\0UNICODE_ESCAPE :
\u{( HEX_DIGIT_* )1..6}STRING_LITERAL :
"(
~["\IsolatedCR]
| QUOTE_ESCAPE
| ASCII_ESCAPE
| UNICODE_ESCAPE
| STRING_CONTINUE
)*"STRING_CONTINUE :
\followed by \nRAW_STRING_LITERAL :
rRAW_STRING_CONTENTRAW_STRING_CONTENT :
"( ~ IsolatedCR )* (non-greedy)"
|#RAW_STRING_CONTENT#BYTE_LITERAL :
b'( ASCII_FOR_CHAR | BYTE_ESCAPE )'ASCII_FOR_CHAR :
any ASCII (i.e. 0x00 to 0x7F), except',/, \n, \r or \tBYTE_ESCAPE :
\xHEX_DIGIT HEX_DIGIT
|\n|\r|\t|\\|\0BYTE_STRING_LITERAL :
b"( ASCII_FOR_STRING | BYTE_ESCAPE | STRING_CONTINUE )*"ASCII_FOR_STRING :
any ASCII (i.e 0x00 to 0x7F), except",/and IsolatedCRRAW_BYTE_STRING_LITERAL :
brRAW_BYTE_STRING_CONTENTRAW_BYTE_STRING_CONTENT :
"ASCII* (non-greedy)"
|#RAW_STRING_CONTENT#ASCII :
any ASCII (i.e. 0x00 to 0x7F)INTEGER_LITERAL :
( DEC_LITERAL | BIN_LITERAL | OCT_LITERAL | HEX_LITERAL ) INTEGER_SUFFIX?DEC_LITERAL :
DEC_DIGIT (DEC_DIGIT|_)*TUPLE_INDEX :
0| NON_ZERO_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]NON_ZERO_DEC_DIGIT : [
1-9]HEX_DIGIT : [
0-9a-fA-F]INTEGER_SUFFIX :
u8|u16|u32|u64|usize
|i8|i16|i32|i64|isizeFLOAT_LITERAL :
DEC_LITERAL.(not immediately followed by.,_or an identifier)
| DEC_LITERAL FLOAT_EXPONENT
| DEC_LITERAL.DEC_LITERAL FLOAT_EXPONENT?
| DEC_LITERAL (.DEC_LITERAL)? FLOAT_EXPONENT? FLOAT_SUFFIXFLOAT_EXPONENT :
(e|E) (+|-)? (DEC_DIGIT|_)* DEC_DIGIT (DEC_DIGIT|_)*FLOAT_SUFFIX :
f32|f64BOOLEAN_LITERAL :
true
|falseLIFETIME_TOKEN
'IDENTIFIER_OR_KEYWORD
|'_LIFETIME_OR_LABEL:
'IDENTIFIEREQ :
=
LT :<
LE :<=
EQEQ :==
NE :!=
GE :>=
GT :>
ANDAND :&&
OROR :||
NOT :!
TILDE :~AT :
@
DOT :.
DOTDOT :..
DOTDOTDOT :...
COMMA :,
SEMI :;
COLON ::
MOD_SEPARATOR :::
RIGHT_ARROW :->
LEFT_ARROW :<-
FAT_ARROW :=>
POUND :#
DOLLAR :$
QUESTION :?UNDERSCORE :
_
LIFETIME_OR_LABEL :'IDENTIFIEROPEN_PAREN :
(
CLOSE_PAREN :)
OPEN_BRACKET :[
CLOSE_BRACKET :]
OPEN_BRACE :{
CLOSE_BRACE :}
Crates and source files
Syntactical productions
Crates and source files
Crate :
UTF8BOM?
SHEBANG?
InnerAttribute*
[Item]*
Items
Item:
OuterAttribute* Visibility
(
Module
| ExternCrate
| UseDeclaration
| [Function]
| TypeAlias
| Struct
| Enumeration
| Union
| ConstantItem
| StaticItem
| Trait
| Implementation
| ExternBlock
| Macro
| MacroDefinition
)Generics :
<GenericParams?>GenericParams :
LifetimeParams,?
| TypeParams,?
| LifetimeParams,TypeParams,?LifetimeParams :
LifetimeParam (,LifetimeParam)*LifetimeParam :
LIFETIME_OR_LABEL LifetimeBounds?TypeParams:
TypeParam (,TypeParam)*TypeParam :
IDENTIFIER TypeParamBounds? (=Type )?TypeParamBounds :
LIFETIME_OR_LABEL
|(LIFETIME_OR_LABEL)
|?? LateBoundLifetimeDefs? TypePath
|(?? LateBoundLifetimeDefs? TypePath)LifetimeBounds :
:LIFETIME_OR_LABEL (+LIFETIME_OR_LABEL )*+?LateBoundLifetimeDefs :
for<LifetimeParams,?>WhereClause :
where( WhereClauseItem (,WhereClauseItem )*,? )?WhereClauseItem :
LifetimeWhereClauseItem
| TypeBoundWhereClauseItem
| TypeEqualWhereClauseItemLifetimeWhereClauseItem :
LIFETIME_OR_LABEL LifetimeBoundsTypeBoundWhereClauseItem :
LateBoundLifetimeDefs? Type:TypeParamBoundsTypeEqualWhereClauseItem :
LateBoundLifetimeDefs? Type (=|==) Type
Modules
Module :
modIDENTIFIER;
|modIDENTIFIER{
InnerAttribute*
[Item]*
}
Extern crates
ExternCrate :
externcrateIDENTIFIER (asIDENTIFIER)?;
Use declarations
UseDeclaration :
(Visibility)?useUseTree;UseTree :
(SimplePath?::)?*
| (SimplePath?::)?{(UseTree (,UseTree )*,?)?}
| SimplePathasIDENTIFIER
Functions
Function:
unsafe? (externAbi?)?fnIDENTIFIER Generics?
(FunctionParameters?)FunctionReturnType? WhereClause?
BlockWithInnerAttributesAbi:
STRING_LITERALFunctionParameters:
FunctionParam (,FunctionParam)*,?FunctionParam :
Pattern:TypeFunctionReturnType:
->Type
Type aliases
TypeAlias :
typeIDENTIFIER Generics? WhereClause?=Type;
Structs
Struct :
StructStruct
| TupleStructStructStruct :
structIDENTIFIER Generics? WhereClause? ({StructFields?}|;)TupleStruct :
structIDENTIFIER Generics?(TupleFields?)WhereClause?;StructFields :
StructField (,StructField)*,?StructField :
OuterAttribute*
Visibility IDENTIFIER:TypeTupleFields :
TupleField (,TupleField)*,?
Enumerations
Enumeration :
enumIDENTIFIER Generics? WhereClause?{EnumItems?}EnumItems :
EnumItem (,EnumItem )*,?EnumItem :
OuterAttribute*
IDENTIFIER ( EnumItemTuple | EnumItemStruct | EnumItemDiscriminant )?EnumItemTuple :
(TupleFields?)EnumItemStruct :
{StructFields?}
Unions
Union :
unionIDENTIFIER Generics? WhereClause?{StructFields}
Constant items
ConstantItem :
constIDENTIFIER:Type=Expression;
Static items
StaticItem :
staticmut? IDENTIFIER:Type=Expression;
Traits
Trait :
unsafe?traitIDENTIFIER [GenericsDecl]? WhereClause?{
TraitItem*
}TraitItem :
TraitMethod | TraitConst | TraitTypeTraitMethod :
TypeMethod | Method
Implementations
Implementation :
unsafe?impl[GenericsDecl] (!? [Path]for)? (TypeName |..){
InnerAttributes?
ImplementationItems?
}
External blocks
ExternBlock :
extern[Abi]?{
InnerAttribute*
ExternalItem*
}ExternalItem :
OuterAttribute*
[VisibilityNoTuple]?
( ExternalStaticItem | ExternalFunctionItem )ExternalStaticItem :
staticmut? IDENTIFIER:Type;ExternalFunctionItem :
fnIDENTIFIER Generics?
( [FunctionParameters] | FunctionParametersWithVariadics )
[FunctionReturnType]? WhereClause?;FunctionParametersWithVariadics :
(( FunctionParam,)* VariadicFunctionParam)VariadicFunctionParam :
FunctionParam,...
Visibility and Privacy
Visibility :
EMPTY
|pub
|pub(crate)
|pub(inModulePath)
|pub(in?self)
|pub(in?super)
Attributes
Attribute :
InnerAttribute | OuterAttributeInnerAttribute :
#![MetaItem]OuterAttribute :
#[MetaItem]MetaItem :
IDENTIFIER
| IDENTIFIER=LITERAL
| IDENTIFIER(LITERAL)
| IDENTIFIER(MetaSeq)
| IDENTIFIER(MetaSeq,)MetaSeq :
EMPTY
| MetaItem
| MetaSeq,MetaItem
Statements
Statement :
DeclarationStatement | Expression |;DeclarationStatement :
[Item] | LocalVariablesDeclarationFIXME
LocalVariablesDeclaration :
letPattern (:Type )? (=Expression )?;ExpressionStatement :
ExpressionWithBlock
| ExpressionWithoutBlock;ExpressionWithBlock :
BlockExpression
| LoopExpression
| IfExpression
| IfLetExpression
| MatchExpression
| WhileExpression
| WhileLetExpressionExpressionWithoutBlock: all other expression types
Expressions
Expression :
LiteralExpression
| [PathExpression]
| BlockExpression
| OperatorExpression
| GroupedExpression
| ArrayExpression
| IndexExpression
| [TupleExpression]
| [TupleIndexingExpression]
| [StructExpression]
| [EnumerationVariantExpression]
| CallExpression
| [MethodCallExpression]
| FieldExpression
| ClosureExpression
| LoopExpression
| ContinueExpression
| BreakExpression
| RangeExpression
| IfExpression
| IfLetExpression
| MatchExpression
| ReturnExpression
Literal expressions
LiteralExpression :
CHAR_LITERAL
| STRING_LITERAL
| RAW_STRING_LITERAL
| BYTE_LITERAL
| BYTE_STRING_LITERAL
| RAW_BYTE_STRING_LITERAL
| INTEGER_LITERAL
| FLOAT_LITERAL
| BOOLEAN_LITERAL
Block expressions
BlockExpression :
{
InnerAttribute*
Statement*
Expression?
}UnsafeBlockExpression :
unsafeBlockExpression
Operator expressions
OperatorExpression :
BorrowExpression
| DereferenceExpression
| ErrorPropagationExpression
| NegationExpression
| ArithmeticOrLogicalExpression
| ComparisonExpression
| LazyBooleanExpression
| TypeCastExpression
| AssignmentExpression
| CompoundAssignmentExpressionBorrowExpression :
(&|&&) Expression
| (&|&&)mutExpressionDereferenceExpression :
*ExpressionErrorPropagationExpression :
Expression?NegationExpression :
-Expression
|!ExpressionArithmeticOrLogicalExpression :
Expression+Expression
| Expression-Expression
| Expression*Expression
| Expression/Expression
| Expression%Expression
| Expression&Expression
| Expression|Expression
| Expression^Expression
| Expression<<Expression
| Expression>>ExpressionComparisonExpression :
Expression==Expression
| Expression!=Expression
| Expression>Expression
| Expression<Expression
| Expression>=Expression
| Expression<=ExpressionLazyBooleanExpression :
Expression||Expression
| Expression&&ExpressionTypeCastExpression :
Expressionas[PathInExpression]AssignmentExpression :
| Expression=ExpressionCompoundAssignmentExpression :
Expression+=Expression
| Expression-=Expression
| Expression*=Expression
| Expression/=Expression
| Expression%=Expression
| Expression&=Expression
| Expression|=Expression
| Expression^=Expression
| Expression<<=Expression
| Expression>>=Expression
Grouped expressions
GroupedExpression :
(Expression)
Array and index expressions
ArrayExpression :
[]
|[Expression (,Expression )*,?]
|[Expression;Expression]
Call expressions
CallExpression :
Expression(CallParams?)CallParams :
Expression (,Expression )*,?
Field access expressions
Closure expressions
ClosureExpression :
move?
(||||[FunctionParameters]?|)
(Expression |->TypeNoBounds BlockExpression)
Loop expressions
LoopExpression :
LoopLabel? (
InfiniteLoopExpression
| PredicateLoopExpression
| PredicatePatternLoopExpression
| IteratorLoopExpression
)InfiniteLoopExpression :
loopBlockExpressionPredicateLoopExpression :
whileExpressionexcept struct expression BlockExpressionPredicatePatternLoopExpression :
whileletPattern=Expressionexcept struct expression BlockExpressionIteratorLoopExpression :
forPatterninExpressionexcept struct expression BlockExpressionLoopLabel :
LIFETIME_OR_LABEL:BreakExpression :
breakLIFETIME_OR_LABEL? Expression?ContinueExpression :
continueLIFETIME_OR_LABEL?
Range expressions
RangeExpression :
RangeExpr
| RangeFromExpr
| RangeToExpr
| RangeFullExprRangeExpr :
Expression..ExpressionRangeFromExpr :
Expression..RangeToExpr :
..ExpressionRangeFullExpr :
..
If and if let expressions
IfExpression :
ifExpressionexcept struct expression BlockExpression
(else( BlockExpression | IfExpression | IfLetExpression ) )?IfLetExpression :
ifletPattern=Expressionexcept struct expression BlockExpression
(else( BlockExpression | IfExpression | IfLetExpression ) )?
Match expressions
MatchExpression :
matchExpressionexcept struct expression{
InnerAttribute*
MatchArms?
}MatchArms :
( MatchArm=>( BlockExpression,? | Expression,) )*
MatchArm=>( BlockExpression | Expression ),?MatchArm :
OuterAttribute* MatchArmPatterns MatchArmGuardMatchArmPatterns :
|? Pattern (|Pattern )*MatchArmGuard :
ifExpression
Return expressions
ReturnExpression :
returnExpression?
Paths
SimplePath :
::? PathSegmentIdentifier (::PathSegmentIdentifier)*PathSegmentIdentifier :
IDENTIFIER |super|self|SelfFIXMEPathInExpr :
::? PathSegmentIdentifier (::(PathSegmentIdentifier | GenericsForType) )*QualifiedPathInExpr :
<type_ (asTypePath)?>::PathInExprTypePath :
::? TypePathElement (::TypePathElement)*TypePathElement :
PathSegmentIdentifier (GenericsForType | FunctionSignature)?GenericsForType :
<(LifetimeParams (,TypeParamsForTypes)? (,BindingParams)?,? )?>
|<TypeParamsForTypes (,BindingParams)?,?>
|<BindingParams,?>TypeParamsForTypes :
Type (,Type)*BindingParams :
TypeBindingParam (,TypeBindingParam)*TypeBindingParam :
IDENTIFIER=type_ FIXMEQualified_TypePath_ :
<Type (asTypePath)?>::TypePath FIXME
Patterns
Pattern :
LiteralPattern
| WildcardPattern
| RangePattern
| ReferencePattern
| IdentifierPattern
| StructPattern
| TuplePattern
| TupleStructPattern
| PathPatternLiteralPattern :
BOOLEAN_LITERAL
| CHAR_LITERAL
| BYTE_LITERAL
| STRING_LITERAL
| RAW_STRING_LITERAL
| BYTE_STRING_LITERAL
| RAW_BYTE_STRING_LITERAL
|-? INTEGER_LITERAL
|-? FLOAT_LITERALWildcardPattern :
_RangePattern :
RangePatternBound...RangePatternBoundRangePatternBound :
CHAR_LITERAL
| BYTE_LITERAL
|-? INTEGER_LITERAL
|-? FLOAT_LITERAL
| [PathInExpression]
| [QualifiedPathInExpression]ReferencePattern :
(&|&&)mut? PatternIdentifierPattern :
mut? IDENTIFIER (@Pattern ) ?
|refmut? IDENTIFIER (@Pattern ) ?StructPattern :
Path{
StructPatternElements ?
}StructPatternElements :
StructPatternFields (,|,StructPatternEtCetera)?
| StructPatternEtCeteraStructPatternFields :
StructPatternField (,StructPatternField) *StructPatternField :
OuterAttribute *
(
INTEGER_LITERAL:Pattern
| IDENTIFIER:Pattern
|ref?mut? IDENTIFIER
)StructPatternEtCetera :
OuterAttribute *
..TupleStructPattern :
Path(TupleStructItems)TupleStructItems :
Pattern (,Pattern )*,?
| (Pattern,)*..( (,Pattern)+,? )?TuplePattern :
(TupplePatternItems?)TuplePatternItems :
Pattern,
| Pattern (,Pattern)+,?
| (Pattern,)*..( (,Pattern)+,? )?PathPattern :
PathForExpression
| QualifiedPathForExpression
Types
Type :
ParenthesizedType (+TypeParamBounds)?
| TupleType
| NeverType
| RawPointerType
| ReferenceType
| ArrayType
| SliceType
| InferredType
| [QualifiedTypePath]
| TypePath (+TypeParamBounds)?
| [BareFunctionType]
| [MacroInvocationType]
| TypeParamBoundsTypeNoBounds :
ParenthesizedType
| TupleType
| NeverType
| RawPointerType
| ReferenceType
| ArrayType
| SliceType
| InferredType
| [QualifiedTypePath]
| TypePath
| [BareFunctionType]
| [MacroInvocationType]ParenthesizedType :
(Type)TupleType :
()
|(Type,)
|(Type (,Type ) +,?)ArrayType :
[_type;Expression]FIXMESliceType :
[_type]FIXMENeverType :
!InferredType :
_ReferenceType :
&Lifetime?mut? _type FIXMERawPointerType :
*(mut|const) _type **FIXMETraitObjectType :
LifetimeOrPath (+LifetimeOrPath )*+?LifetimeOrPath : [Path] | [LIFETIME_OR_LABEL]