Rust Operators

Rust Operators – Complete Beginner Guide
Operators are the building blocks of logic in Rust.
They allow you to:
Perform calculations
Compare values
Make decisions
Assign variables
Work with Booleans
Manipulate bits
Control program flow
If you want to master Rust, you must understand operators deeply.
In this complete beginner guide, you’ll learn:
What operators are in Rust
Arithmetic operators
Comparison operators
Logical operators
Assignment operators
Bitwise operators
Range operators
Operator precedence
Common mistakes
Best practices
Real-world examples
Let’s dive in
What Are Operators in Rust?
Operators are special symbols that perform operations on values (operands).
Example:
1 | let sum = 5 + 3; |
Here:
5and3are operands+is the operator
Rust supports multiple categories of operators.
Arithmetic Operators in Rust
Arithmetic operators are used for mathematical calculations.
List of Arithmetic Operators
| Operator | Meaning |
|---|---|
+ | Addition |
- | Subtraction |
* | Multiplication |
/ | Division |
% | Modulus (remainder) |
Addition (+)
1 2 3 4 | fn main() { let result = 10 + 5; println!("{}", result); } |
Output:
Subtraction (-)
1 | let result = 10 - 3; |
Multiplication (*)
1 | let result = 4 * 6; |
Division (/)
1 | let result = 10 / 2; |
Important:
Integer division removes decimal part.
Use
f32orf64for floating-point division.
Example:
1 2 | let result = 7 / 2; // 3 let result = 7.0 / 2.0; // 3.5 |
Modulus (%)
Returns remainder.
1 | let remainder = 10 % 3; // 1 |
Used for:
Checking even/odd
Cyclic counters
Algorithms
Comparison Operators
Comparison operators return Boolean values (true or false).
| Operator | Meaning |
|---|---|
== | Equal to |
!= | Not equal |
> | Greater than |
< | Less than |
>= | Greater than or equal |
<= | Less than or equal |
Example
1 2 3 4 | fn main() { let age = 20; println!("{}", age >= 18); } |
Output:
All comparison operators return a bool.
Logical Operators
Used to combine Boolean expressions.
| Operator | Meaning |
|---|---|
&& | AND |
| ` | |
! | NOT |
AND (&&)
Both conditions must be true.
1 2 3 | if age >= 18 && has_id { println!("Access granted"); } |
OR (||)
At least one must be true.
1 2 3 | if is_weekend || is_holiday { println!("Relax!"); } |
NOT (!)
Reverses Boolean value.
1 2 3 | if !is_logged_in { println!("Login required"); } |
Short-Circuit Evaluation
Rust stops evaluation early when possible.
1 2 | if false && expensive_function() { } |
The second condition is never evaluated.
This improves performance and safety.
Assignment Operators
Used to assign or update values.
| Operator | Meaning |
|---|---|
= | Assign |
+= | Add and assign |
-= | Subtract and assign |
*= | Multiply and assign |
/= | Divide and assign |
%= | Modulus and assign |
Basic Assignment
1 | let x = 10; |
Compound Assignment
1 2 | let mut x = 5; x += 3; // x = x + 3 |
After execution:
Important:
You must use mut to modify variables.
Bitwise Operators
Used for low-level programming.
| Operator | Meaning |
|---|---|
& | Bitwise AND |
| ` | ` |
^ | Bitwise XOR |
<< | Left shift |
>> | Right shift |
Example
1 2 3 4 | let a = 5; // 0101 let b = 3; // 0011 let result = a & b; // 0001 (1) |
Bitwise operators are useful in:
Systems programming
Embedded development
Cryptography
Performance optimization
Range Operators
Rust has powerful range operators.
| Operator | Meaning |
|---|---|
.. | Exclusive range |
..= | Inclusive range |
Exclusive Range
1 2 3 | for i in 1..5 { println!("{}", i); } |
Output:
2
3
4
5 is excluded.
Inclusive Range
1 2 3 | for i in 1..=5 { println!("{}", i); } |
Output:
2
3
4
5
Ranges are widely used in loops and slicing.
Operator Precedence in Rust
Operator precedence determines evaluation order.
Example:
1 | let result = 2 + 3 * 4; |
Multiplication happens first.
Result:
To change order:
1 | let result = (2 + 3) * 4; |
Result:
Use parentheses for clarity.
Operators with Different Types
Rust does NOT automatically convert types.
Invalid:
1 | let result = 5 + 3.2; |
Correct:
1 | let result = 5.0 + 3.2; |
Rust enforces strict type safety.
Operators in Real-World Example
Example: Simple Discount Calculator
1 2 3 4 5 6 7 8 | fn main() { let price = 100.0; let discount = 0.2; let final_price = price - (price * discount); println!("Final price: {}", final_price); } |
Operators used:
-*=
This is real business logic.
Common Beginner Mistakes
Using = instead of ==
Wrong:
1 2 | if x = 5 { } |
Correct:
1 2 | if x == 5 { } |
Forgetting mut keyword
1 2 | let x = 5; x += 2; // Error |
Must use:
1 | let mut x = 5; |
Mixing integer and float types
Rust requires explicit matching types.
Best Practices for Using Operators
- Use parentheses for clarity
- Keep expressions simple
- Avoid overly complex conditions
- Use descriptive variable names
- Break long logic into smaller variables
Example:
1 2 3 4 5 | let is_eligible = age >= 18 && has_license; if is_eligible { println!("Approved"); } |
Much cleaner than writing the full expression repeatedly.
Advanced Tip: Operator Overloading
Rust allows operator overloading using traits like Add, Sub, etc.
Example concept:
1 | use std::ops::Add; |
Advanced topic, but important for custom types.
Frequently Asked Questions (FAQs)
1. What are operators in Rust?
Operators are symbols that perform operations on values, such as arithmetic, comparison, logical, and assignment operations.
2. Do comparison operators return Boolean values?
Yes. All comparison operators return true or false.
3. Does Rust support compound assignment operators?
Yes. Rust supports +=, -=, *=, /=, and %=.
4. What is the difference between && and &?
&&is logical AND (for Booleans)&is bitwise AND (for integers)
5. Does Rust automatically convert types in operations?
No. Rust is strictly typed and does not perform implicit type conversions.
Final Thoughts
Rust operators are:
Powerful
Strict
Type-safe
Essential for real-world programming
They control:
Calculations
Logic
Comparisons
Loops
System-level behavior
Mastering operators builds a strong foundation for:
Control flow
Functions
Ownership
Advanced Rust concepts
If you understand operators clearly, you’re well on your way to becoming a confident Rust developer.
