B - Operators and Symbols
Appendix B: Operators and Symbols
This appendix contains a glossary of Rust’s syntax, including operators and other symbols that appear by themselves or in the context of paths, generics, trait bounds, macros, attributes, comments, tuples, and brackets.
Operators
Table B-1 contains the operators in Rust, an example of how the operator would appear in context, a short explanation, and whether that operator is overloadable. If an operator is overloadable, the relevant trait to use to overload that operator is listed.
Table B-1: Operators
OperatorExampleExplanationOverloadable?!``ident!(...)
, ident!{...}
, ident![...]
Macro expansion!``!expr
Bitwise or logical complementNot``!=``expr != expr
Nonequality comparisonPartialEq``%``expr % expr
Arithmetic remainderRem``%=``var %= expr
Arithmetic remainder and assignmentRemAssign``&``&expr
, &mut expr
Borrow&``&type
, &mut type
, &'a type
, &'a mut type
Borrowed pointer type&``expr & expr
Bitwise ANDBitAnd``&=``var &= expr
Bitwise AND and assignmentBitAndAssign``&&``expr && expr
Short-circuiting logical AND*``expr * expr
Arithmetic multiplicationMul``*=``var *= expr
Arithmetic multiplication and assignmentMulAssign``*``*expr
DereferenceDeref``*``*const type
, *mut type
Raw pointer+``trait + trait
, 'a + trait
Compound type constraint+``expr + expr
Arithmetic additionAdd``+=``var += expr
Arithmetic addition and assignmentAddAssign``,``expr, expr
Argument and element separator-``- expr
Arithmetic negationNeg``-``expr - expr
Arithmetic subtractionSub``-=``var -= expr
Arithmetic subtraction and assignmentSubAssign``->``fn(...) -> type
, |…| -> type
Function and closure return type.``expr.ident
Member access..``..
, expr..
, ..expr
, expr..expr
Right-exclusive range literalPartialOrd``..=``..=expr
, expr..=expr
Right-inclusive range literalPartialOrd``..``..expr
Struct literal update syntax..``variant(x, ..)
, struct_type { x, .. }
“And the rest” pattern binding...``expr...expr
(Deprecated, use ..=
instead) In a pattern: inclusive range pattern/``expr / expr
Arithmetic divisionDiv``/=``var /= expr
Arithmetic division and assignmentDivAssign``:``pat: type
, ident: type
Constraints:``ident: expr
Struct field initializer:``'a: loop {...}
Loop label;``expr;
Statement and item terminator;``[...; len]
Part of fixed-size array syntax<<``expr << expr
Left-shiftShl``<<=``var <<= expr
Left-shift and assignmentShlAssign``<``expr < expr
Less than comparisonPartialOrd``<=``expr <= expr
Less than or equal to comparisonPartialOrd``=``var = expr
, ident = type
Assignment/equivalence==``expr == expr
Equality comparisonPartialEq``=>``pat => expr
Part of match arm syntax>``expr > expr
Greater than comparisonPartialOrd``>=``expr >= expr
Greater than or equal to comparisonPartialOrd``>>``expr >> expr
Right-shiftShr``>>=``var >>= expr
Right-shift and assignmentShrAssign``@``ident @ pat
Pattern binding^``expr ^ expr
Bitwise exclusive ORBitXor``^=``var ^= expr
Bitwise exclusive OR and assignmentBitXorAssign``|``pat | pat
Pattern alternatives|``expr | expr
Bitwise ORBitOr``|=``var |= expr
Bitwise OR and assignmentBitOrAssign``||``expr || expr
Short-circuiting logical OR?``expr?
Error propagation
Non-operator Symbols
The following list contains all symbols that don’t function as operators; that is, they don’t behave like a function or method call.
Table B-2 shows symbols that appear on their own and are valid in a variety of locations.
Table B-2: Stand-Alone Syntax
SymbolExplanation'ident
Named lifetime or loop label...u8
, ...i32
, ...f64
, ...usize
, etc.Numeric literal of specific type"..."
String literalr"..."
, r#"..."#
, r##"..."##
, etc.Raw string literal, escape characters not processedb"..."
Byte string literal; constructs an array of bytes instead of a stringbr"..."
, br#"..."#
, br##"..."##
, etc.Raw byte string literal, combination of raw and byte string literal'...'
Character literalb'...'
ASCII byte literal|…| expr
Closure!
Always empty bottom type for diverging functions_
“Ignored” pattern binding; also used to make integer literals readable
Table B-3 shows symbols that appear in the context of a path through the module hierarchy to an item.
Table B-3: Path-Related Syntax
SymbolExplanationident::ident
Namespace path::path
Path relative to the extern prelude, where all other crates are rooted (i.e., an explicitly absolute path including crate name)self::path
Path relative to the current module (i.e., an explicitly relative path).super::path
Path relative to the parent of the current moduletype::ident
, <type as trait>::ident
Associated constants, functions, and types<type>::...
Associated item for a type that cannot be directly named (e.g., <&T>::...
, <[T]>::...
, etc.)trait::method(...)
Disambiguating a method call by naming the trait that defines ittype::method(...)
Disambiguating a method call by naming the type for which it’s defined<type as trait>::method(...)
Disambiguating a method call by naming the trait and type
Table B-4 shows symbols that appear in the context of using generic type parameters.
Table B-4: Generics
SymbolExplanationpath<...>
Specifies parameters to generic type in a type (e.g., Vec<u8>
)path::<...>
, method::<...>
Specifies parameters to generic type, function, or method in an expression; often referred to as turbofish (e.g., "42".parse::<i32>()
)fn ident<...> ...
Define generic functionstruct ident<...> ...
Define generic structureenum ident<...> ...
Define generic enumerationimpl<...> ...
Define generic implementationfor<...> type
Higher-ranked lifetime boundstype<ident=type>
A generic type where one or more associated types have specific assignments (e.g., Iterator<Item=T>
)
Table B-5 shows symbols that appear in the context of constraining generic type parameters with trait bounds.
Table B-5: Trait Bound Constraints
SymbolExplanationT: U
Generic parameter T
constrained to types that implement U``T: 'a
Generic type T
must outlive lifetime 'a
(meaning the type cannot transitively contain any references with lifetimes shorter than 'a
)T: 'static
Generic type T
contains no borrowed references other than 'static
ones'b: 'a
Generic lifetime 'b
must outlive lifetime 'a``T: ?Sized
Allow generic type parameter to be a dynamically sized type'a + trait
, trait + trait
Compound type constraint
Table B-6 shows symbols that appear in the context of calling or defining macros and specifying attributes on an item.
Table B-6: Macros and Attributes
SymbolExplanation#[meta]
Outer attribute#![meta]
Inner attribute$ident
Macro substitution$ident:kind
Macro capture$(…)…
Macro repetitionident!(...)
, ident!{...}
, ident![...]
Macro invocation
Table B-7 shows symbols that create comments.
Table B-7: Comments
SymbolExplanation//
Line comment//!
Inner line doc comment///
Outer line doc comment/*...*/
Block comment/*!...*/
Inner block doc comment/**...*/
Outer block doc comment
Table B-8 shows symbols that appear in the context of using tuples.
Table B-8: Tuples
SymbolExplanation()
Empty tuple (aka unit), both literal and type(expr)
Parenthesized expression(expr,)
Single-element tuple expression(type,)
Single-element tuple type(expr, ...)
Tuple expression(type, ...)
Tuple typeexpr(expr, ...)
Function call expression; also used to initialize tuple struct
s and tuple enum
variantsexpr.0
, expr.1
, etc.Tuple indexing
Table B-9 shows the contexts in which curly braces are used.
Table B-9: Curly Brackets
ContextExplanation{...}
Block expressionType {...}``struct
literal
Table B-10 shows the contexts in which square brackets are used.
Table B-10: Square Brackets
ContextExplanation[...]
Array literal[expr; len]
Array literal containing len
copies of expr``[type; len]
Array type containing len
instances of type``expr[expr]
Collection indexing. Overloadable ( Index
, IndexMut
)expr[..]
, expr[a..]
, expr[..b]
, expr[a..b]
Collection indexing pretending to be collection slicing, using Range
, RangeFrom
, RangeTo
, or RangeFull
as the “index”