|
| 1 | +alias:: [[Rust Operators]] |
| 2 | +- # Operator |
| 3 | + - Operators in Rust are special symbols or keywords that perform operations on variables and values |
| 4 | + - Source: [Appendix B: Operators and Symbols - The Rust Programming Language](https://rust-book.cs.brown.edu/appendix-02-operators.html) |
| 5 | + - ## Overview |
| 6 | + - Rust contains a glossary of operators and other symbols that appear by themselves or in the context of paths, generics, trait bounds, macros, attributes, comments, tuples, and brackets |
| 7 | + - Many operators can be overloaded through traits |
| 8 | + - ## Operators |
| 9 | + - ### Arithmetic Operators |
| 10 | + - `+` - Arithmetic addition (overloadable via `Add` trait) |
| 11 | + - `+=` - Arithmetic addition and assignment (overloadable via `AddAssign` trait) |
| 12 | + - `-` - Arithmetic negation (overloadable via `Neg` trait) or arithmetic subtraction (overloadable via `Sub` trait) |
| 13 | + - `-=` - Arithmetic subtraction and assignment (overloadable via `SubAssign` trait) |
| 14 | + - `*` - Arithmetic multiplication (overloadable via `Mul` trait) or dereference (overloadable via `Deref` trait) |
| 15 | + - `*=` - Arithmetic multiplication and assignment (overloadable via `MulAssign` trait) |
| 16 | + - `/` - Arithmetic division (overloadable via `Div` trait) |
| 17 | + - `/=` - Arithmetic division and assignment (overloadable via `DivAssign` trait) |
| 18 | + - `%` - Arithmetic remainder (overloadable via `Rem` trait) |
| 19 | + - `%=` - Arithmetic remainder and assignment (overloadable via `RemAssign` trait) |
| 20 | + - ### Comparison Operators |
| 21 | + - `==` - Equality comparison (overloadable via `PartialEq` trait) |
| 22 | + - `!=` - Nonequality comparison (overloadable via `PartialEq` trait) |
| 23 | + - `<` - Less than comparison (overloadable via `PartialOrd` trait) |
| 24 | + - `<=` - Less than or equal to comparison (overloadable via `PartialOrd` trait) |
| 25 | + - `>` - Greater than comparison (overloadable via `PartialOrd` trait) |
| 26 | + - `>=` - Greater than or equal to comparison (overloadable via `PartialOrd` trait) |
| 27 | + - ### Logical Operators |
| 28 | + - `&&` - Short-circuiting logical AND (not overloadable) |
| 29 | + - `||` - Short-circuiting logical OR (not overloadable) |
| 30 | + - `!` - Bitwise or logical complement (overloadable via `Not` trait) |
| 31 | + - ### Bitwise Operators |
| 32 | + - `&` - Bitwise AND (overloadable via `BitAnd` trait) or borrow (`&expr`, `&mut expr` - not overloadable) |
| 33 | + - `&=` - Bitwise AND and assignment (overloadable via `BitAndAssign` trait) |
| 34 | + - `|` - Bitwise OR (overloadable via `BitOr` trait) or pattern alternatives (`pat | pat` - not overloadable) |
| 35 | + - `|=` - Bitwise OR and assignment (overloadable via `BitOrAssign` trait) |
| 36 | + - `^` - Bitwise exclusive OR (overloadable via `BitXor` trait) |
| 37 | + - `^=` - Bitwise exclusive OR and assignment (overloadable via `BitXorAssign` trait) |
| 38 | + - `<<` - Left-shift (overloadable via `Shl` trait) |
| 39 | + - `<<=` - Left-shift and assignment (overloadable via `ShlAssign` trait) |
| 40 | + - `>>` - Right-shift (overloadable via `Shr` trait) |
| 41 | + - `>>=` - Right-shift and assignment (overloadable via `ShrAssign` trait) |
| 42 | + - ### Assignment and Other Operators |
| 43 | + - `=` - Assignment/equivalence (not overloadable) |
| 44 | + - `.` - Field access, method call, or tuple indexing (not overloadable) |
| 45 | + - `..` - Right-exclusive range literal (overloadable via `PartialOrd` trait) or struct literal update syntax |
| 46 | + - `..=` - Right-inclusive range literal (overloadable via `PartialOrd` trait) |
| 47 | + - `->` - Function and closure return type (not overloadable) |
| 48 | + - `?` - Error propagation (not overloadable) |
| 49 | + - `@` - Pattern binding (not overloadable) |
| 50 | + - `:` - Constraints, struct field initializer, or loop label (not overloadable) |
| 51 | + - `;` - Statement and item terminator or part of fixed-size array syntax (not overloadable) |
| 52 | + - `,` - Argument and element separator (not overloadable) |
| 53 | + - `=>` - Part of match arm syntax (not overloadable) |
| 54 | + - ## Operator Overloading |
| 55 | + - Many operators in Rust can be overloaded through traits |
| 56 | + - This allows custom types to use standard operator syntax |
| 57 | + - Common overloadable operator traits include: |
| 58 | + - `Add`, `Sub`, `Mul`, `Div`, `Rem` for arithmetic operations |
| 59 | + - `AddAssign`, `SubAssign`, `MulAssign`, `DivAssign`, `RemAssign` for assignment operations |
| 60 | + - `PartialEq`, `PartialOrd` for comparisons |
| 61 | + - `BitAnd`, `BitOr`, `BitXor`, `Shl`, `Shr` for bitwise operations |
| 62 | + - `Not`, `Neg` for unary operations |
| 63 | + - `Deref` for dereferencing |
| 64 | + - ## Non-operator Symbols |
| 65 | + - Rust also includes many symbols that don't function as operators |
| 66 | + - These include symbols for: |
| 67 | + - Paths through the module hierarchy |
| 68 | + - Generic type parameters |
| 69 | + - Trait bound constraints |
| 70 | + - Macros and attributes |
| 71 | + - Comments |
| 72 | + - Tuples, arrays, and brackets |
| 73 | + - ## Related |
| 74 | + - [[Programming/Operator]] - General programming concept of operators |
| 75 | + - [[Rust/Trait]] - Traits used for operator overloading |
| 76 | + - [[Rust/Book/TRPL]] - The Rust Programming Language book |
| 77 | + - [[Programming/Language/Feature]] - Language features in general |
| 78 | + |
0 commit comments