Types of Operators in Rust
Rust categorizes its operators into the following types:
- Arithmetic Operators
- Comparison (Relational) Operators
- Logical Operators
- Bitwise Operators
- Assignment Operators
- Compound Assignment Operators
- Miscellaneous Operators
1. Arithmetic Operators
Arithmetic operators are used for mathematical calculations.
Operator | Description | Example | Output |
---|---|---|---|
+ | Addition | 5 + 3 | 8 |
– | Subtraction | 5 – 3 | 2 |
* | Multiplication | 5 * 3 | 15 |
/ | Division (Integer/F64) | 5 / 2 | 2 (int) |
% | Modulus (Remainder) | 5 % 2 | 1 |
Example:
fn main() {
let a = 10;
let b = 3;
println!("Addition: {}", a + b);
println!("Subtraction: {}", a - b);
println!("Multiplication: {}", a * b);
println!("Division: {}", a / b);
println!("Remainder: {}", a % b);
}
2. Comparison (Relational) Operators
Comparison operators are used to compare two values and return a Boolean result (true or false).
Operator | Description | Example | Output |
---|---|---|---|
== | Equal to | 5 == 5 | true |
!= | Not equal to | 5 != 3 | true |
> | Greater than | 5 > 3 | true |
< | Less than | 5 < 3 | false |
>= | Greater than or equal to | 5 >= 5 | true |
<= | Less than or equal to | 3 <= 5 | true |
Example:
fn main() {
let x = 10;
let y = 20;
println!("x == y: {}", x == y);
println!("x != y: {}", x != y);
println!("x > y: {}", x > y);
println!("x < y: {}", x < y);
println!("x >= y: {}", x >= y);
println!("x <= y: {}", x <= y);
}
3. Logical Operators
Logical operators are used to combine or negate Boolean expressions.
Operator | Description | Example | Output |
---|---|---|---|
&& | Logical AND | true && false | false |
` | ` | Logical OR | |
! | Logical NOT | !true | false |
Example:
fn main() {
let x = true;
let y = false;
println!("x && y: {}", x && y);
println!("x || y: {}", x || y);
println!("!x: {}", !x);
}
4. Bitwise Operators
Bitwise operators work on binary representations of integers.
Operator | Description | Example | Output |
---|---|---|---|
& | Bitwise AND | 5 & 3 | 1 |
` | ` | Bitwise OR | `5 |
^ | Bitwise XOR | 5 ^ 3 | 6 |
<< | Left Shift | 5 << 1 | 10 |
>> | Right Shift | 5 >> 1 | 2 |
Example:
fn main() {
let a = 5; // 0101 in binary
let b = 3; // 0011 in binary
println!("Bitwise AND: {}", a & b);
println!("Bitwise OR: {}", a | b);
println!("Bitwise XOR: {}", a ^ b);
println!("Left Shift: {}", a << 1);
println!("Right Shift: {}", a >> 1);
}
5. Assignment Operators
Assignment operators assign values to variables.
Operator | Description | Example | Output |
---|---|---|---|
= | Assign | let x = 5 | x = 5 |
Example:
fn main() {
let x = 10; // Assigning 10 to x
println!("Value of x: {}", x);
}
6. Compound Assignment Operators
These operators combine basic operations with assignment.
Operator | Description | Example | Equivalent To |
---|---|---|---|
+= | Add and assign | x += 5 | x = x + 5 |
-= | Subtract and assign | x -= 5 | x = x – 5 |
*= | Multiply and assign | x *= 5 | x = x * 5 |
/= | Divide and assign | x /= 5 | x = x / 5 |
%= | Modulus and assign | x %= 5 | x = x % 5 |
Example:
fn main() {
let mut x = 10;
x += 5;
println!("After addition: {}", x);
x *= 2;
println!("After multiplication: {}", x);
}
7. Miscellaneous Operators
Operator | Description | Example | Output |
---|---|---|---|
. | Access a field or method | my_struct.x | Access x |
-> | Access a pointer | ptr->field | Access field |