Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt

Use this file to discover all available pages before exploring further.

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)
let x: i32 = 10;
let y: i32 = 5;
let z: i32 = 2;

// Syntactic representation
let a = x + y;
let b = -z;

// Underlying trait resolution
let a_trait = std::ops::Add::add(x, y);
let b_trait = std::ops::Neg::neg(z);

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)
let x: i32 = 0b1010;
let y: i32 = 0b1100;

// Syntactic representation
let a = x ^ y;
let b = x << 2;
let c = !x;

// Underlying trait resolution
let a_trait = std::ops::BitXor::bitxor(x, y);
let b_trait = std::ops::Shl::shl(x, 2);
let c_trait = std::ops::Not::not(x);

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)
let x: bool = true;
let y: bool = false;

// Syntactic representation
let a = x && y;
let b = !x;

// Underlying trait resolution for NOT
let b_trait = std::ops::Not::not(x);

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)
let x: i32 = 10;
let y: i32 = 20;

// Syntactic representation
let a = x == y;
let b = x >= y;

// Underlying trait resolution
let a_trait = std::cmp::PartialEq::eq(&x, &y);
let b_trait = std::cmp::PartialOrd::ge(&x, &y);

Compound Assignment Operators

Compound assignment combines an arithmetic or bitwise operation with variable assignment. These require mutable bindings and map to specific *Assign traits.
  • +=, -=, *=, /=, %=
  • &=, |=, ^=, <<=, >>=
let mut x: i32 = 5;
let y: i32 = 10;

// Syntactic representation
x += y;

// Underlying trait resolution
std::ops::AddAssign::add_assign(&mut x, y);

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)
let mut arr = [10, 20, 30];

// Syntactic representation
let val = arr[1];
arr[1] = 25;

// Underlying trait resolution
let val_trait = *std::ops::Index::index(&arr, 1);
*std::ops::IndexMut::index_mut(&mut arr, 1) = 25;

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.).
fn structural_operators() -> Result<(), std::num::ParseIntError> {
    // Borrowing and Primitive Dereferencing (Built-in compiler operation)
    let mut val: i32 = 42;
    let ptr: &mut i32 = &mut val;
    let deref_val = *ptr; 

    // Custom Type Dereferencing (Trait resolution + primitive dereference)
    let rc_val = std::rc::Rc::new(10);
    let unboxed = *rc_val; // Resolves to: *(std::ops::Deref::deref(&rc_val))

    // Type Casting
    let int_val: i32 = 10;
    let float_val: f64 = int_val as f64;

    // Error Propagation (Requires a compatible return type like Result)
    let parsed_val: i32 = "100".parse()?; 

    // Ranges
    let exclusive = 0..5;   // std::ops::Range { start: 0, end: 5 }
    let inclusive = 0..=5;  // std::ops::RangeInclusive::new(0, 5)

    Ok(())
}
Master Rust with Deep Grasping Methodology!Learn More