Appendix B - Operators and Symbols - The Rust Programming Language
Appendix B - Operators and Symbols - The Rust Programming Language
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.
Bitwise or logical
! !expr Not
complement
!= expr != expr Nonequality comparison PartialEq
&type , &mut
type , &'a
& Borrowed pointer type
type , &'a mut
type
Arithmetic multiplication
*= var *= expr MulAssign
and assignment
* *expr Dereference Deref
Operator Example Explanation Overloadable?
*const type ,
* Raw pointer
*mut type
trait + trait ,
+ Compound type constraint
'a + trait
..=expr ,
..= Right-inclusive range literal PartialOrd
expr..=expr
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-3 shows symbols that appear in the context of a path through the module hierarchy
to an item.
Symbol Explanation
ident::ident Namespace path
Path relative to the crate root (i.e., an explicitly
::path
absolute path)
Path relative to the current module (i.e., an
self::path
explicitly relative path).
super::path Path relative to the parent of the current module
type::ident , <type as
Associated constants, functions, and types
trait>::ident
Symbol Explanation
Specifies parameters to generic type in a type (e.g.,
path<...>
Vec<u8> )
Table B-5 shows symbols that appear in the context of constraining generic type parameters
with trait bounds.
Symbol Explanation
T: U Generic parameter T constrained to types that implement U
Table B-6 shows symbols that appear in the context of calling or defining macros and
specifying attributes on an item.
Symbol Explanation
#[meta] Outer attribute
Symbol Explanation
#![meta] Inner attribute
$ident Macro substitution
$ident:kind Macro capture
$(…)… Macro repetition
ident!(...) , ident!{...} , ident![...] Macro invocation
Symbol Explanation
// 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.
Symbol Explanation
() 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 type
Function call expression; also used to initialize tuple struct s
expr(expr, ...)
and tuple enum variants
expr.0 , expr.1 ,
Tuple indexing
etc.
Table B-9 shows the contexts in which curly braces are used.
Context Explanation
{...} Block expression
Context Explanation
Type {...} struct literal
Table B-10 shows the contexts in which square brackets are used.
Context Explanation
[...] Array literal
[expr; len] Array literal containing len copies of expr