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 symbolic tokens that instruct the compiler to perform specific mathematical, logical, relational, or bitwise computations. Unlike many systems programming languages, Rust implements most operators as syntactic sugar for trait methods defined in the std::ops and std::cmp modules. This architecture allows operators to be overloaded for custom types by implementing the corresponding traits.

Arithmetic Operators

Arithmetic operators perform standard mathematical computations and map directly to traits in std::ops. For integer types, if an operation overflows in debug mode, Rust will panic; in release mode, it performs two’s complement wrapping. Floating-point types (f32, f64) follow IEEE 754 rules and will evaluate to infinity (inf or -inf) upon overflow, without panicking or wrapping in either debug or release mode.
  • + : Addition (std::ops::Add)
  • - : Subtraction (std::ops::Sub)
  • * : Multiplication (std::ops::Mul)
  • / : Division (std::ops::Div)
  • % : Remainder (std::ops::Rem)
  • - : Negation (std::ops::Neg)
let a = 10_i32 + 5_i32;  // Evaluated as 10_i32.add(5_i32)
let b = 10_i32 - 5_i32;  // Evaluated as 10_i32.sub(5_i32)
let c = 10_i32 * 5_i32;  // Evaluated as 10_i32.mul(5_i32)
let d = 10_i32 / 5_i32;  // Evaluated as 10_i32.div(5_i32)
let e = 10_i32 % 5_i32;  // Evaluated as 10_i32.rem(5_i32)
let f = -a;              // Evaluated as a.neg()

let g = 1e308_f64 * 10.0; // Evaluated as inf (IEEE 754 overflow)

Bitwise and Eager Logical Operators

Bitwise operators manipulate the individual bits of integer types. Additionally, the & and | operators are implemented for bool types to perform eager (non-short-circuiting) logical evaluation. They map to traits in std::ops. The ! operator is overloaded to function as a bitwise NOT when applied to integers.
  • & : Bitwise AND / Eager Logical AND (std::ops::BitAnd)
  • | : Bitwise OR / Eager Logical 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 and_val = 0b1010_u8 & 0b1100_u8; // Evaluated as 0b1010_u8.bitand(0b1100_u8)
let or_val  = 0b1010_u8 | 0b1100_u8; // Evaluated as 0b1010_u8.bitor(0b1100_u8)
let xor_val = 0b1010_u8 ^ 0b1100_u8; // Evaluated as 0b1010_u8.bitxor(0b1100_u8)
let not_val = !0b1010_u8;            // Evaluated as 0b1010_u8.not()
let shl_val = 0b1010_u8 << 2_u8;     // Evaluated as 0b1010_u8.shl(2_u8)
let shr_val = 0b1010_u8 >> 2_u8;     // Evaluated as 0b1010_u8.shr(2_u8)

// Eager logical evaluation (both sides are evaluated regardless of the left side's value)
let eager_and = false & true;        // Evaluated as false.bitand(true)
let eager_or  = true | false;        // Evaluated as true.bitor(false)

Logical Operators

Logical operators evaluate boolean expressions. The && and || operators utilize short-circuit evaluation. Because short-circuiting requires controlling control flow, && and || cannot be overloaded and do not have backing traits. The ! operator functions as a logical NOT when applied to booleans.
  • && : Logical AND (Short-circuiting)
  • || : Logical OR (Short-circuiting)
  • ! : Logical NOT (std::ops::Not)
let is_true = (true && false) || true;
let is_false = !is_true; // Evaluated as is_true.not()

Comparison (Relational) Operators

Comparison operators evaluate the relationship between two values, returning a boolean. They map to traits in std::cmp.
  • == : 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 eq = 5_i32 == 5_i32; // Evaluated via PartialEq::eq
let ne = 5_i32 != 5_i32; // Evaluated via PartialEq::ne
let lt = 5_i32 < 10_i32; // Evaluated via PartialOrd::lt

Assignment and Compound Assignment Operators

The basic assignment operator (=) binds a value to a memory location or updates a mutable variable. Compound assignment operators combine an arithmetic or bitwise operation with assignment. Compound operators map to traits in std::ops suffixed with Assign. They mutate the left-hand operand in place and do not return a value (they evaluate to the unit type ()).
  • = : Basic Assignment
  • +=, -=, *=, /=, %= : Arithmetic Compound Assignment
  • &=, |=, ^=, <<=, >>= : Bitwise Compound Assignment
let mut x;
x = 5_i32;   // Basic assignment

x += 5_i32;  // Evaluated as x.add_assign(5_i32)
x <<= 1_i32; // Evaluated as x.shl_assign(1_i32)

Type, Memory, and Access Operators

Rust includes specific operators for memory management, pointer manipulation, collection access, and type conversion.
  • & : Shared borrow / Reference creation
  • &mut : Mutable borrow / Mutable reference creation
  • * : Dereference (std::ops::Deref and std::ops::DerefMut)
  • [] : Array/Slice indexing (std::ops::Index and std::ops::IndexMut)
  • as : Safe primitive type casting
let val = 10;
let ref_val = &val;         // Shared reference

let mut mut_val = 20;       // Must be declared mutable to borrow mutably
let mut_ref = &mut mut_val; // Mutable reference

let deref_val = *ref_val;   // Dereferencing

let arr = [10, 20, 30];
let item = arr[1];          // Evaluated via Index::index

let float_val = 10 as f64;  // Type cast

Control Flow and Structural Operators

These operators handle structural generation or control flow propagation at the syntax level.
  • ? : Error propagation operator. Unwraps Result/Option or returns early on Err/None (std::ops::Try).
  • .. : Exclusive range (std::ops::Range)
  • ..= : Inclusive range (std::ops::RangeInclusive)
fn some_fallible_function() -> Result<i32, &'static str> {
    Ok(42)
}

fn main() -> Result<(), &'static str> {
    let range_ex = 0..5;   // 0, 1, 2, 3, 4
    let range_in = 0..=5;  // 0, 1, 2, 3, 4, 5

    // Error propagation syntax requires a compatible return type
    let result = some_fallible_function()?; 
    
    Ok(())
}
Master Rust with Deep Grasping Methodology!Learn More