Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Syntax index

This appendix provides an index of tokens and common forms with links to where those elements are defined.

Keywords

KeywordUse
_wildcard pattern, inferred const, inferred type, placeholder lifetime, constant items, extern crate, use declarations, destructuring assignment
abstractreserved keyword
asextern crate, use declarations, type cast expressions, qualified paths
asyncasync functions, async blocks, async closures
awaitawait expressions
becomereserved keyword
boxreserved keyword
breakbreak expressions
constconst functions, const items, const generics, const blocks, raw borrow operator, raw pointer type, const assembly operands
continuecontinue expressions
crateextern crate, visibility, paths
doreserved keyword
dyntrait objects
elselet statements, if expressions
enumenumerations
externextern crate, extern function qualifier, external blocks, extern function pointer types
falseboolean type, boolean expressions, configuration predicates
finalreserved keyword
fnfunctions, function pointer types
fortrait implementations, iterator loops, higher-ranked trait bounds
genreserved keyword
ifif expressions, match guards
implinherent impls, trait impls, impl trait types, anonymous type parameters
invisibility, iterator loops, assembly operands
letlet statements, if let patterns
loopinfinite loops
macro_rulesmacros by example
macroreserved keyword
matchmatch expressions
modmodules
moveclosure expressions, async blocks
mutborrow expressions, identifier patterns, reference patterns, struct patterns, reference types, raw pointer types, self parameters, static items
overridereserved keyword
privreserved keyword
pubvisibility
rawborrow expressions, raw assembly
refidentifier patterns, struct patterns
returnreturn expressions
safeexternal block functions, external block statics
selfextern crate, self parameters, visibility, self paths
SelfSelf type paths, use bounds
staticstatic items, 'static lifetimes
structstructs
supersuper paths, visibility
traittrait items
trueboolean type, boolean expressions, configuration predicates
tryreserved keyword
typetype aliases
typeofreserved keyword
unionunion items
unsafeunsafe blocks, unsafe attributes, unsafe modules, unsafe functions, unsafe external blocks, unsafe external functions, unsafe external statics, unsafe traits, unsafe trait implementations
unsizedreserved keyword
useuse items, use bounds
virtualreserved keyword
wherewhere clauses
whilepredicate loops
yieldreserved keyword

Operators and punctuation

SymbolNameUse
+Plusaddition, trait bounds, macro Kleene matcher
-Minussubtraction, negation
*Starmultiplication, dereference, raw pointers, macro Kleene matcher, glob imports
/Slashdivision
%Percentremainder
^Caretbitwise and logical XOR
!Notbitwise and logical NOT, macro calls, inner attributes, never type, negative impls
&Andbitwise and logical AND, borrow, references, reference patterns
|Orbitwise and logical OR, closures, or patterns, if let, while let
&&AndAndlazy AND, borrow, references, reference patterns
||OrOrlazy OR, closures
<<Shlshift left, nested generics
>>Shrshift right, nested generics
+=PlusEqaddition assignment
-=MinusEqsubtraction assignment
*=StarEqmultiplication assignment
/=SlashEqdivision assignment
%=PercentEqremainder assignment
^=CaretEqbitwise XOR assignment
&=AndEqbitwise AND assignment
|=OrEqbitwise OR assignment
<<=ShlEqshift left assignment
>>=ShrEqshift right assignment, nested generics
=Eqassignment, let statements, attributes, various type definitions
==EqEqequal
!=Nenot equal
>Gtgreater than, generics, paths, use bounds
<Ltless than, generics, paths, use bounds
>=Gegreater than or equal to, generics
<=Leless than or equal to
@Atsubpattern binding
.Dotfield access, tuple index
..DotDotrange expressions, struct expressions, rest pattern, range patterns, struct patterns
...DotDotDotvariadic functions, range patterns
..=DotDotEqinclusive range expressions, range patterns
,Commavarious separators
;Semiterminator for various items and statements, array expressions, array types
:Colonvarious separators
::PathSeppath separator
->RArrowfunctions, closures, function pointer type
=>FatArrowmatch arms, macros
<-LArrowThe left arrow symbol has been unused since before Rust 1.0, but it is still treated as a single token.
#Poundattributes, raw string literals, raw byte string literals, raw C string literals
$Dollarmacros
?Questiontry propagation expressions, relaxed trait bounds, macro Kleene matcher
~TildeThe tilde operator has been unused since before Rust 1.0, but its token may still be used.

Comments

Other tokens

TokenUse
identidentifiers
r#identraw identifiers
'identlifetimes and loop labels
'r#identraw lifetimes and loop labels
…u8, …i32, …f64, …usize, …number literals
"…"string literals
r"…", r#"…"#, r##"…"##, …raw string literals
b"…"byte string literals
br"…", br#"…"#, br##"…"##, …raw byte string literals
'…'character literals
b'…'byte literals
c"…"C string literals
cr"…", cr#"…"#, cr##"…"##, …raw C string literals

Macros

SyntaxUse
ident!(…)
ident! {…}
ident![…]
macro invocations
$identmacro metavariable
$ident:kindmacro matcher fragment specifier
$(…)…macro repetition

Attributes

SyntaxUse
#[meta]outer attribute
#![meta]inner attribute

Expressions

ExpressionUse
|…| expr
|…| -> Type { … }
closures
ident::…paths
::crate_name::…explicit crate paths
crate::…crate-relative paths
self::…module-relative paths
super::…parent module paths
Type::…
<Type as Trait>::ident
associated items
<Type>::…qualified paths which can be used for types without names such as <&T>::…, <[T]>::…, etc.
Trait::method(…)
Type::method(…)
<Type as Trait>::method(…)
disambiguated method calls
method::<…>(…)
path::<…>
generic arguments, aka turbofish
()unit
(expr)parenthesized expressions
(expr,)single-element tuple expressions
(expr, …)tuple expressions
expr(expr, …)call expressions
expr.0, expr.1, …tuple indexing expressions
expr.identfield access expressions
{…}block expressions
Type {…}struct expressions
Type(…)tuple struct constructors
[…]array expressions
[expr; len]repeat array expressions
expr[..], expr[a..], expr[..b], expr[a..b], expr[a..=b], expr[..=b]array and slice indexing expressions
if expr {…} else {…}if expressions
match expr { pattern => {…} }match expressions
loop {…}infinite loop expressions
while expr {…}predicate loop expressions
for pattern in expr {…}iterator loops
&expr
&mut expr
borrow expressions
&raw const expr
&raw mut expr
raw borrow expressions
*exprdereference expressions
expr?try propagation expressions
-exprnegation expressions
!exprbitwise and logical NOT expressions
expr as Typetype cast expressions

Items

Items are the components of a crate.

ItemUse
mod ident;
mod ident {…}
modules
use path;use declarations
fn ident(…) {…}functions
type Type = Type;type aliases
struct ident {…}structs
enum ident {…}enumerations
union ident {…}unions
trait ident {…}traits
impl Type {…}
impl Type for Trait {…}
implementations
const ident = expr;constant items
static ident = expr;static items
extern "C" {…}external blocks
fn ident<…>(…) …
struct ident<…> {…}
enum ident<…> {…}
impl<…> Type<…> {…}
generic definitions

Type expressions

Type expressions are used to refer to types.

TypeUse
bool, u8, f64, str, …primitive types
for<…>higher-ranked trait bounds
T: TraitA + TraitBtrait bounds
T: 'a + 'blifetime bounds
T: TraitA + 'atrait and lifetime bounds
T: ?Sizedrelaxed trait bounds
[Type; len]array types
(Type, …)tuple types
[Type]slice types
(Type)parenthesized types
impl Traitimpl trait types, anonymous type parameters
dyn Traittrait object types
ident
ident::…
type paths (can refer to structs, enumerations, unions, type aliases, traits, generics, etc.)
Type<…>
Trait<…>
generic arguments (e.g. Vec<u8>)
Trait<ident = Type>associated type bindings (e.g. Iterator<Item = T>)
Trait<ident: …>associated type bounds (e.g. Iterator<Item: Send>)
&Type
&mut Type
reference types
*mut Type
*const Type
raw pointer types
fn(…) -> Typefunction pointer types
_inferred type, inferred const
'_placeholder lifetime
!never type

Patterns

Patterns are used to match values.

PatternUse
"foo", 'a', 123, 2.4, …literal patterns
identidentifier patterns
_wildcard pattern
..rest pattern
a.., ..b, a..b, a..=b, ..=brange patterns
&pattern
&mut pattern
reference patterns
path {…}struct patterns
path(…)tuple struct patterns
(pattern, …)tuple patterns
(pattern)grouped patterns
[pattern, …]slice patterns
CONST, Enum::Variant, …path patterns