Rust Operators

Performing operations and calculations in Rust

⚡ What are Rust Operators?

Operators in Rust are symbols that perform operations on variables and values. They include arithmetic, comparison, logical, and assignment operators for mathematical calculations, comparisons, and data manipulation.


// Basic operators in action
let sum = 10 + 5;        // Addition
let is_equal = 10 == 10; // Comparison
let result = true && false; // Logical
                                    
Operator Types

Types of Operators

Arithmetic

Mathematical operations

let sum = 5 + 3;     // 8
let diff = 10 - 4;   // 6
let product = 6 * 7; // 42
⚖️

Comparison

Comparing values

let is_equal = 5 == 5;    // true
let is_greater = 10 > 5;  // true
let is_less = 3 < 8;      // true
🧠

Logical

Boolean operations

let and_result = true && false; // false
let or_result = true || false;  // true
let not_result = !true;         // false
📝

Assignment

Assigning and modifying values

let mut x = 5;
x += 3;  // x = x + 3 = 8
x *= 2;  // x = x * 2 = 16

🔹 Arithmetic Operators

Basic mathematical operations in Rust:

fn main() {
    let a = 20;
    let b = 6;
    
    // Basic arithmetic
    let addition = a + b;       // 26
    let subtraction = a - b;    // 14
    let multiplication = a * b; // 120
    let division = a / b;       // 3 (integer division)
    let remainder = a % b;      // 2 (modulo)
    
    println!("Addition: {} + {} = {}", a, b, addition);
    println!("Subtraction: {} - {} = {}", a, b, subtraction);
    println!("Multiplication: {} * {} = {}", a, b, multiplication);
    println!("Division: {} / {} = {}", a, b, division);
    println!("Remainder: {} % {} = {}", a, b, remainder);
    
    // Floating point division
    let precise_division = 20.0 / 6.0;
    println!("Precise division: {:.2}", precise_division);
}

Output:

Addition: 20 + 6 = 26

Subtraction: 20 - 6 = 14

Multiplication: 20 * 6 = 120

Division: 20 / 6 = 3

Remainder: 20 % 6 = 2

Precise division: 3.33

🔹 Comparison Operators

Operators for comparing values and returning boolean results:

fn main() {
    let x = 10;
    let y = 20;
    
    // Comparison operators
    let equal = x == y;           // false
    let not_equal = x != y;       // true
    let less_than = x < y;        // true
    let greater_than = x > y;     // false
    let less_equal = x <= y;      // true
    let greater_equal = x >= y;   // false
    
    println!("{} == {} is {}", x, y, equal);
    println!("{} != {} is {}", x, y, not_equal);
    println!("{} < {} is {}", x, y, less_than);
    println!("{} > {} is {}", x, y, greater_than);
    println!("{} <= {} is {}", x, y, less_equal);
    println!("{} >= {} is {}", x, y, greater_equal);
}

Output:

10 == 20 is false

10 != 20 is true

10 < 20 is true

10 > 20 is false

10 <= 20 is true

10 >= 20 is false

🔹 Logical Operators

Operators for combining and manipulating boolean values:

fn main() {
    let is_sunny = true;
    let is_warm = false;
    let is_weekend = true;
    
    // Logical AND (&&)
    let perfect_day = is_sunny && is_warm;
    println!("Perfect day (sunny AND warm): {}", perfect_day);
    
    // Logical OR (||)
    let good_day = is_sunny || is_weekend;
    println!("Good day (sunny OR weekend): {}", good_day);
    
    // Logical NOT (!)
    let not_sunny = !is_sunny;
    println!("Not sunny: {}", not_sunny);
    
    // Complex expressions
    let beach_day = (is_sunny && is_warm) || is_weekend;
    println!("Beach day: {}", beach_day);
    
    let stay_home = !is_sunny && !is_warm;
    println!("Stay home: {}", stay_home);
}

Output:

Perfect day (sunny AND warm): false

Good day (sunny OR weekend): true

Not sunny: false

Beach day: true

Stay home: false

🔹 Assignment Operators

Operators for assigning and modifying variable values:

fn main() {
    let mut score = 100;
    println!("Initial score: {}", score);
    
    // Compound assignment operators
    score += 25;    // score = score + 25
    println!("After += 25: {}", score);
    
    score -= 10;    // score = score - 10
    println!("After -= 10: {}", score);
    
    score *= 2;     // score = score * 2
    println!("After *= 2: {}", score);
    
    score /= 5;     // score = score / 5
    println!("After /= 5: {}", score);
    
    score %= 7;     // score = score % 7
    println!("After %= 7: {}", score);
    
    // Regular assignment
    score = 50;
    println!("After = 50: {}", score);
}

Output:

Initial score: 100

After += 25: 125

After -= 10: 115

After *= 2: 230

After /= 5: 46

After %= 7: 4

After = 50: 50

🧠 Test Your Knowledge

What is the result of 17 % 5 in Rust?