Skip to main content
Operators in Rust are reserved symbols that instruct the compiler to perform specific mathematical, relational, logical, or structural operations on one or more operands. Structurally, most operators in Rust are syntactic sugar for method calls defined by traits in the std::ops and std::cmp modules. This trait-based architecture allows developers to overload operators for custom types by implementing the corresponding trait, while operations on primitive types and certain compiler intrinsics are handled directly by the compiler.

Arithmetic Operators

Arithmetic operators perform standard mathematical computations. They map directly to traits in the std::ops module.
  • + : Addition (std::ops::Add)
  • - : Subtraction (std::ops::Sub) and Negation (std::ops::Neg)
  • * : Multiplication (std::ops::Mul)
  • / : Division (std::ops::Div)
  • % : Remainder (std::ops::Rem)

Bitwise Operators

Bitwise operators manipulate the binary representations of integer types. Unlike C or C++, Rust uses ! for bitwise negation rather than ~.
  • & : Bitwise AND (std::ops::BitAnd)
  • | : Bitwise OR (std::ops::BitOr)
  • ^ : Bitwise XOR (std::ops::BitXor)
  • ! : Bitwise NOT (std::ops::Not)
  • << : Left Shift (std::ops::Shl)
  • >> : Right Shift (std::ops::Shr)

Logical Operators

Logical operators evaluate boolean expressions. The && and || operators utilize short-circuit evaluation; consequently, they are evaluated at the compiler level and cannot be overloaded via traits. The ! operator acts as a Logical NOT when applied to boolean types.
  • && : Logical AND (Short-circuiting, not overloadable)
  • || : Logical OR (Short-circuiting, not overloadable)
  • ! : Logical NOT (std::ops::Not)

Comparison Operators

Comparison operators evaluate the equivalence or relative ordering of two values, returning a bool. They map to traits in the std::cmp module.
  • == : Equal to (std::cmp::PartialEq)
  • != : Not equal to (std::cmp::PartialEq)
  • < : Less than (std::cmp::PartialOrd)
  • > : Greater than (std::cmp::PartialOrd)
  • <= : Less than or equal to (std::cmp::PartialOrd)
  • >= : Greater than or equal to (std::cmp::PartialOrd)

Compound Assignment Operators

Compound assignment combines an arithmetic or bitwise operation with variable assignment. These require mutable bindings and map to specific *Assign traits.
  • +=, -=, *=, /=, %=
  • &=, |=, ^=, <<=, >>=

Indexing Operator

The indexing operator [] provides access to elements within a collection. It maps to traits that return a reference, meaning the syntactic sugar implicitly dereferences the result of the trait method.
  • [] : Immutable Indexing (std::ops::Index)
  • [] : Mutable Indexing (std::ops::IndexMut)

Structural and Memory Operators

Rust includes specific operators for memory management, type coercion, and control flow mechanics.
  • & / &mut : Borrow operator. Creates an immutable or mutable reference.
  • * : Dereference operator. Accesses the value a reference points to. For primitive reference types and compiler-intrinsic types like Box<T>, this is a built-in compiler operation. For standard custom types, it is overloaded via std::ops::Deref and std::ops::DerefMut, which desugar into a trait call followed by a primitive dereference.
  • as : Type cast operator. Performs safe, primitive type conversions (Not overloadable).
  • ? : Error propagation operator. Returns early from a function if the operand is an Err or None variant (std::ops::Try / std::ops::FromResidual).
  • .. / ..= : Range operators. Construct exclusive or inclusive range structs (std::ops::Range, std::ops::RangeInclusive, etc.).
Tired of Poor Rust Skills? Fix That With Deep Grasping!Learn More