Operators and Symbols

Read original on doc.rust-lang.org

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.

Arithmetic Operators

OperatorExampleExplanationOverloadable?
+expr + exprArithmetic additionAdd
+=var += exprArithmetic addition and assignmentAddAssign
--exprArithmetic negationNeg
-expr - exprArithmetic subtractionSub
-=var -= exprArithmetic subtraction and assignmentSubAssign
*expr * exprArithmetic multiplicationMul
*=var *= exprArithmetic multiplication and assignmentMulAssign
/expr / exprArithmetic divisionDiv
/=var /= exprArithmetic division and assignmentDivAssign
%expr % exprArithmetic remainderRem
%=var %= exprArithmetic remainder and assignmentRemAssign

Comparison Operators

OperatorExampleExplanationOverloadable?
==expr == exprEquality comparisonPartialEq
!=expr != exprNonequality comparisonPartialEq
<expr < exprLess than comparisonPartialOrd
<=expr <= exprLess than or equal to comparisonPartialOrd
>expr > exprGreater than comparisonPartialOrd
>=expr >= exprGreater than or equal to comparisonPartialOrd

Logical Operators

OperatorExampleExplanation
!!exprBitwise or logical complement (Not trait)
&&expr && exprShort-circuiting logical AND
||expr || exprShort-circuiting logical OR

Bitwise Operators

OperatorExampleExplanationOverloadable?
&expr & exprBitwise ANDBitAnd
&=var &= exprBitwise AND and assignmentBitAndAssign
|expr | exprBitwise ORBitOr
|=var |= exprBitwise OR and assignmentBitOrAssign
^expr ^ exprBitwise exclusive ORBitXor
^=var ^= exprBitwise exclusive OR and assignmentBitXorAssign

Reference and Pointer Operators

OperatorExampleExplanation
&&expr, &mut exprBorrow
&&type, &mut typeBorrowed pointer type
**exprDereference (Deref trait)
**const type, *mut typeRaw pointer

Other Operators

SymbolExplanation
->Function and closure return type
.Field access, method call
..Range (exclusive), struct update syntax
..=Range (inclusive)
...Variable-length argument lists
::Path separator
:Type annotation, trait bounds
;Statement terminator
,Argument and element separator
=>Match arm syntax
?Error propagation
@Pattern binding
_Ignored pattern binding

Symbols

Brackets and Delimiters

SymbolExplanation
()Empty tuple, function calls, grouping
[]Array/slice indexing, array literals
{}Block expressions, struct literals

Comments

SymbolExplanation
//Line comment
/* */Block comment
///Doc comment (outer)
//!Doc comment (inner)

Attributes

SymbolExplanation
#[...]Outer attribute
#![...]Inner attribute

Macros

SymbolExplanation
ident!(...)Macro invocation (parentheses)
ident![...]Macro invocation (brackets)

Generics and Lifetimes

SymbolExplanation
<T>Generic type parameter
<T: Trait>Generic with trait bound
'aLifetime parameter
'staticStatic lifetime

For the complete reference, see the official Rust documentation.