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
// 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
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
a + b = 13
a % b = 1
a > b = true
access = true
AND = 0010
shifted = 10100Key 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
1. What does the `%` operator return in Rust?
2. Which operator represents logical AND in Rust?
3. Does Rust support operator overloading for custom types by default?
4. What is a key property of Rust's && and || operators?
5. Which operator shifts bits to the left in Rust?
Was this page helpful?
You May Also Like
Variables and Data Types in Rust
Learn how Rust variables are immutable by default and explore its scalar and compound data types.
Type Conversion in Rust
Learn how Rust handles explicit type conversion using the as keyword and the From/Into traits.
if-else Expressions in Rust
Learn how Rust's if-else works as an expression, requires bool conditions, and can return values directly.
Related Reading
Related Study Notes in Programming
Browse all study notesApache Spark Study Notes
Programming · 30 topics
ProgrammingApache Flink Study Notes
Programming · 30 topics
ProgrammingHadoop Study Notes
Programming · 30 topics
ProgrammingSnowflake Study Notes
Programming · 30 topics
ProgrammingApache Airflow Study Notes
Programming · 30 topics
Programmingdbt (Data Build Tool) Study Notes
Programming · 30 topics