0% found this document useful (0 votes)
5 views

Appendix B - Operators and Symbols - The Rust Programming Language

Uploaded by

deltax
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Appendix B - Operators and Symbols - The Rust Programming Language

Uploaded by

deltax
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

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

Operator Example Explanation Overloadable?


ident!(...) ,
! ident!{...} , Macro expansion
ident![...]

Bitwise or logical
! !expr Not
complement
!= expr != expr Nonequality comparison PartialEq

% expr % expr Arithmetic remainder Rem

Arithmetic remainder and


%= var %= expr RemAssign
assignment
&expr , &mut
& Borrow
expr

&type , &mut
type , &'a
& Borrowed pointer type
type , &'a mut
type

& expr & expr Bitwise AND BitAnd

Bitwise AND and


&= var &= expr BitAndAssign
assignment
&& expr && expr Short-circuiting logical AND
* expr * expr Arithmetic multiplication Mul

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 + expr Arithmetic addition Add

Arithmetic addition and


+= var += expr AddAssign
assignment
Argument and element
, expr, expr
separator
- - expr Arithmetic negation Neg

- expr - expr Arithmetic subtraction Sub

Arithmetic subtraction and


-= var -= expr SubAssign
assignment
fn(...) ->
Function and closure return
-> type , |...| ->
type
type

. expr.ident Member access


.. , expr.. ,
.. ..expr , Right-exclusive range literal PartialOrd
expr..expr

..=expr ,
..= Right-inclusive range literal PartialOrd
expr..=expr

.. ..expr Struct literal update syntax


variant(x,
..) , “And the rest” pattern
..
struct_type { binding
x, .. }

(Deprecated, use ..=


... expr...expr instead) In a pattern:
inclusive range pattern
/ expr / expr Arithmetic division Div

Arithmetic division and


/= var /= expr DivAssign
assignment
pat: type ,
: Constraints
ident: type

: ident: expr Struct field initializer


'a: loop
: Loop label
{...}
Operator Example Explanation Overloadable?
Statement and item
; expr;
terminator
Part of fixed-size array
; [...; len]
syntax
<< expr << expr Left-shift Shl

<<= var <<= expr Left-shift and assignment ShlAssign

< expr < expr Less than comparison PartialOrd

Less than or equal to


<= expr <= expr PartialOrd
comparison
var = expr ,
= Assignment/equivalence
ident = type

== expr == expr Equality comparison PartialEq

=> pat => expr Part of match arm syntax


> expr > expr Greater than comparison PartialOrd

Greater than or equal to


>= expr >= expr PartialOrd
comparison
>> expr >> expr Right-shift Shr

>>= var >>= expr Right-shift and assignment ShrAssign

@ ident @ pat Pattern binding


^ expr ^ expr Bitwise exclusive OR BitXor

Bitwise exclusive OR and


^= var ^= expr BitXorAssign
assignment
| pat | pat Pattern alternatives
| expr | expr Bitwise OR BitOr

|= var |= expr Bitwise OR and assignment BitOrAssign

|| 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


Symbol Explanation
'ident Named lifetime or loop label
...u8 , ...i32 , ...f64 ,
Numeric literal of specific type
...usize , etc.

"..." String literal


r"..." , r#"..."# , Raw string literal, escape characters not
r##"..."## , etc. processed
Byte string literal; constructs an array of bytes
b"..."
instead of a string
br"..." , br#"..."# , Raw byte string literal, combination of raw and
br##"..."## , etc. byte string literal
'...' Character literal
b'...' 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

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

Associated item for a type that cannot be directly


<type>::...
named (e.g., <&T>::... , <[T]>::... , etc.)
Disambiguating a method call by naming the trait
trait::method(...)
that defines it
Disambiguating a method call by naming the type
type::method(...)
for which it’s defined
<type as Disambiguating a method call by naming the trait
trait>::method(...) and type
Table B-4 shows symbols that appear in the context of using generic type parameters.

Table B-4: Generics

Symbol Explanation
Specifies parameters to generic type in a type (e.g.,
path<...>
Vec<u8> )

Specifies parameters to generic type, function, or method


path::<...> ,
in an expression; often referred to as turbofish (e.g.,
method::<...>
"42".parse::<i32>() )

fn ident<...> ... Define generic function


struct ident<...>
Define generic structure
...

enum ident<...> ... Define generic enumeration


impl<...> ... Define generic implementation
for<...> type Higher-ranked lifetime bounds
A generic type where one or more associated types have
type<ident=type>
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

Symbol Explanation
T: U Generic parameter T constrained to types that implement U

Generic type T must outlive lifetime 'a (meaning the type


T: 'a cannot transitively contain any references with lifetimes shorter
than 'a )
Generic type T contains no borrowed references other than
T: 'static
'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 ,
Compound type constraint
trait + trait

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

Symbol Explanation
#[meta] Outer attribute
Symbol Explanation
#![meta] Inner attribute
$ident Macro substitution
$ident:kind Macro capture
$(…)… Macro repetition
ident!(...) , ident!{...} , ident![...] Macro invocation

Table B-7 shows symbols that create comments.

Table B-7: Comments

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.

Table B-8: 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.

Table B-9: Curly Brackets

Context Explanation
{...} Block expression
Context Explanation
Type {...} struct literal

Table B-10 shows the contexts in which square brackets are used.

Table B-10: Square Brackets

Context Explanation
[...] 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 )

Collection indexing pretending to be collection slicing,


expr[..] , expr[a..] ,
using Range , RangeFrom , RangeTo , or RangeFull as
expr[..b] , expr[a..b]
the “index”

You might also like