Operators and Symbols
Read original on doc.rust-lang.orgAppendix 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
| Operator | Example | Explanation | Overloadable? |
|---|---|---|---|
+ | expr + expr | Arithmetic addition | Add |
+= | var += expr | Arithmetic addition and assignment | AddAssign |
- | -expr | Arithmetic negation | Neg |
- | expr - expr | Arithmetic subtraction | Sub |
-= | var -= expr | Arithmetic subtraction and assignment | SubAssign |
* | expr * expr | Arithmetic multiplication | Mul |
*= | var *= expr | Arithmetic multiplication and assignment | MulAssign |
/ | expr / expr | Arithmetic division | Div |
/= | var /= expr | Arithmetic division and assignment | DivAssign |
% | expr % expr | Arithmetic remainder | Rem |
%= | var %= expr | Arithmetic remainder and assignment | RemAssign |
Comparison Operators
| Operator | Example | Explanation | Overloadable? |
|---|---|---|---|
== | expr == expr | Equality comparison | PartialEq |
!= | expr != expr | Nonequality comparison | PartialEq |
< | expr < expr | Less than comparison | PartialOrd |
<= | expr <= expr | Less than or equal to comparison | PartialOrd |
> | expr > expr | Greater than comparison | PartialOrd |
>= | expr >= expr | Greater than or equal to comparison | PartialOrd |
Logical Operators
| Operator | Example | Explanation |
|---|---|---|
! | !expr | Bitwise or logical complement (Not trait) |
&& | expr && expr | Short-circuiting logical AND |
|| | expr || expr | Short-circuiting logical OR |
Bitwise Operators
| Operator | Example | Explanation | Overloadable? |
|---|---|---|---|
& | expr & expr | Bitwise AND | BitAnd |
&= | var &= expr | Bitwise AND and assignment | BitAndAssign |
| | expr | expr | Bitwise OR | BitOr |
|= | var |= expr | Bitwise OR and assignment | BitOrAssign |
^ | expr ^ expr | Bitwise exclusive OR | BitXor |
^= | var ^= expr | Bitwise exclusive OR and assignment | BitXorAssign |
Reference and Pointer Operators
| Operator | Example | Explanation |
|---|---|---|
& | &expr, &mut expr | Borrow |
& | &type, &mut type | Borrowed pointer type |
* | *expr | Dereference (Deref trait) |
* | *const type, *mut type | Raw pointer |
Other Operators
| Symbol | Explanation |
|---|---|
-> | 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
| Symbol | Explanation |
|---|---|
() | Empty tuple, function calls, grouping |
[] | Array/slice indexing, array literals |
{} | Block expressions, struct literals |
Comments
| Symbol | Explanation |
|---|---|
// | Line comment |
/* */ | Block comment |
/// | Doc comment (outer) |
//! | Doc comment (inner) |
Attributes
| Symbol | Explanation |
|---|---|
#[...] | Outer attribute |
#![...] | Inner attribute |
Macros
| Symbol | Explanation |
|---|---|
ident!(...) | Macro invocation (parentheses) |
ident![...] | Macro invocation (brackets) |
Generics and Lifetimes
| Symbol | Explanation |
|---|---|
<T> | Generic type parameter |
<T: Trait> | Generic with trait bound |
'a | Lifetime parameter |
'static | Static lifetime |
For the complete reference, see the official Rust documentation.