100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace
Rust

Operators in Rust

Explore Rust's arithmetic, comparison, logical, and bitwise operators with practical examples.

BasicsBeginner8 min readJul 8, 2026
Analogies

Introduction

Rust provides a familiar set of operators for performing computations and comparisons: arithmetic operators for math, comparison operators for evaluating relationships between values, logical operators for combining boolean expressions, and bitwise operators for manipulating individual bits. Rust does not support operator overloading by default for arbitrary types; however, operators like + can be enabled for custom types by implementing traits such as Add from std::ops, a more advanced topic covered separately.

🏏

Cricket analogy: Run-rate math uses the same arithmetic operators as any calculation, comparing two batting averages uses standard comparison operators, and a custom 'PartnershipScore' struct needs an explicit Add trait before you can '+' two partnerships together.

Syntax

rust
// Arithmetic
let sum = 5 + 3;
let diff = 5 - 3;
let product = 5 * 3;
let quotient = 5 / 3;
let remainder = 5 % 3;

// Comparison
let is_equal = (5 == 5);
let is_greater = (5 > 3);

// Logical
let result = (true && false) || !false;

// Bitwise
let a = 5 & 3;
let b = 5 | 3;
let c = 5 ^ 3;
let d = 5 << 1;

Explanation

Arithmetic operators (+, -, *, /, %) behave as expected on numeric types, with integer division truncating toward zero. Comparison operators (==, !=, <, >, <=, >=) return a bool. Logical operators && (AND), || (OR), and ! (NOT) operate on boolean values and short-circuit evaluation, meaning && skips the right side if the left is false, and || skips the right side if the left is true. Bitwise operators &, |, ^, <<, and >> work on the binary representation of integers. Because Rust does not overload operators automatically for custom types, using + on a user-defined struct requires explicitly implementing the corresponding trait.

🏏

Cricket analogy: Integer division of overs bowled truncates toward zero just like 7/2 balls gives 3 not 3.5; a captain's DRS check with '&&' short-circuits and skips checking the second condition if the first (ball tracking) already fails; comparing two scores with '>' returns a bool for who's ahead.

Example

rust
fn main() {
    let a = 10;
    let b = 3;

    println!("a + b = {}", a + b);
    println!("a % b = {}", a % b);
    println!("a > b = {}", a > b);

    let logged_in = true;
    let has_token = false;
    println!("access = {}", logged_in && !has_token);

    let flags = 0b1010;
    let mask = 0b0110;
    println!("AND = {:04b}", flags & mask);
    println!("shifted = {:04b}", flags << 1);
}

Output

text
a + b = 13
a % b = 1
a > b = true
access = true
AND = 0010
shifted = 10100

Key Takeaways

  • Arithmetic operators include +, -, *, /, and % (remainder).
  • Comparison operators like ==, !=, <, > return a bool.
  • Logical operators && and || short-circuit; ! negates a bool.
  • Bitwise operators &, |, ^, <<, >> operate on integer bit patterns.
  • Rust does not overload operators by default; custom behavior requires implementing traits like Add.

Practice what you learned

Was this page helpful?

Topics covered

#Rust#RustProgrammingStudyNotes#Programming#OperatorsInRust#Operators#Syntax#Explanation#Example#StudyNotes#SkillVeris