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:
- () – parentheses
- ! – not
- *, /, %
- +, –
- <, >, <=, >=
- ==, !=
- &&
- ||
- 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
Rust Learning Path
- Introduction to Rust
- Setting Up Rust Development Environment
- Your First Rust Program
- Variables and Mutability in Rust
- Constants and Immutability in Rust
- Type Annotations and Inference in Rust
- Variable Shadowing and Scope in Rust
- Rust Primitive Data Types
- Working with Strings in Rust
- Every Operator in Rust Explained
- Tuples in Rust
- Arrays and Slices in Rust
- Rust Decision Making: if & match
- Loops in Rust
- Reading Input from the Console in Rust
- Mastering Rust Functions (For Beginners)
- Understanding Ownership in Rust
- Borrowing and References in Rust
- Copy Types vs. Move Semantics in Rust
- Cloning Data in Rust
- Methods and Associated Functions in Rust
- Enums: Defining Variant Types in Rust
- Pattern Matching with Enums in Rust
- The Option Type: Null Safety in Rust
- Error Handling with Result
- Unrecoverable Errors in Rust
- Organizing Code with Modules in Rust
- Cargo Basics: Rust Package Manager
- How to Use External Crates in Rust