Every Operator in Rust Explained

Rust supports standard operators for arithmetic (+, , *, /), comparison (==, !=, <, >), and logic (&&, ||, !). These operators work on numbers, booleans, and some custom types, making them essential for performing calculations and decisions in your code.

In this post, I will explain how to use operators in Rust. If you are coming from JavaScript, many of them will look familiar, but Rust is more strict about how and where you use them. This clarity is what makes Rust less error-prone.

Operators in Rust are used to do things like add numbers, compare values, and control logic. They work on built-in types and can also be used in custom ways with traits, which we will cover later.

Arithmetic Operators

These are used for simple math:

let sum = 5 + 3;      // addition
let diff = 10 - 4;    // subtraction
let product = 4 * 2;  // multiplication
let quotient = 20 / 5; // division
let remainder = 7 % 3; // modulo

Rust uses integer division when both numbers are integers. So 20 / 6 gives 3, not 3.33.

If you want decimal results, use f32 or f64:

let result = 10.0 / 4.0; // gives 2.5

Comparison Operators

Used to compare values and return a bool:

let is_equal = 5 == 5;       // true
let is_not_equal = 5 != 3;   // true
let greater = 10 > 7;        // true
let less = 2 < 5;            // true
let greater_or_equal = 6 >= 6; // true
let less_or_equal = 4 <= 9;    // true

Rust does not allow you to compare values of different types without converting them first. This avoids confusion.

Logical Operators

Used to combine boolean expressions:

let a = true;
let b = false;

let and = a && b; // false
let or = a || b;  // true
let not = !a;     // false

These are helpful inside if statements and while making decisions.

Example:

if age > 18 && has_id {
    println!("You may enter.");
}

Assignment Operators

Rust also supports compound assignment:

let mut x = 10;
x += 5; // same as x = x + 5
x -= 3;
x *= 2;
x /= 4;
x %= 3;

These require the variable to be mutable, otherwise the compiler will throw an error.

Bitwise Operators

Rust includes operators that work at the bit level:

let a = 2;  // 0010
let b = 3;  // 0011

let and = a & b; // 0010 => 2
let or = a | b;  // 0011 => 3
let xor = a ^ b; // 0001 => 1
let shift_left = a << 1; // 0100 => 4
let shift_right = b >> 1; // 0001 => 1

These are rarely needed unless you are doing low-level operations.

Operator Precedence

Rust follows strict order of operations:

  1. () – parentheses
  2. ! – not
  3. *, /, %
  4. +,
  5. <, >, <=, >=
  6. ==, !=
  7. &&
  8. ||
  9. Assignment (=, +=, etc.)

Use parentheses to make your expressions clearer:

let result = (2 + 3) * 4; // 20, not 14

Using Operators in Conditions

Operators are mostly used in if statements:

let age = 21;
let has_ticket = true;

if age >= 18 && has_ticket {
    println!("Allowed to enter.");
} else {
    println!("Entry denied.");
}

Summary

Rust provides all standard operators for math, logic, and comparison. It enforces type safety, so you cannot mix types freely as in JavaScript. Use let mut if you want to use compound assignment. Parentheses help with clarity in complex expressions.

Next up: Strings in Rust