Operators and Expressions - Rust

 

Rust Operators: The Toolbox of Awesomeness

Rust gives you a set of powerful operators to manipulate data, just like a wizard wielding magic spells. Whether you're adding numbers, comparing values, or flipping bits, Rust has the tools you need! 

1. Arithmetic Operators 

Let's start with the basics—math! Rust provides standard arithmetic operators:

fn main() {
    let sum = 10 + 5;      // Addition
    let difference = 10 - 5; // Subtraction
    let product = 10 * 5;   // Multiplication
    let quotient = 10 / 5;  // Division
    let remainder = 10 % 3; // Modulus (Remainder)
    
    println!("Sum: {}", sum);
    println!("Difference: {}", difference);
    println!("Product: {}", product);
    println!("Quotient: {}", quotient);
    println!("Remainder: {}", remainder);
}

Pro Tip: Rust performs integer division when dividing two integers, so 10 / 3 would return 3 instead of 3.33. Use floating-point numbers for precise results!

2. Comparison Operators 

Rust lets you compare values like a detective solving a mystery. Here are the main comparison operators:

fn main() {
    let a = 10;
    let b = 5;
    
    println!("Is a equal to b? {}", a == b);
    println!("Is a not equal to b? {}", a != b);
    println!("Is a greater than b? {}", a > b);
    println!("Is a less than b? {}", a < b);
    println!("Is a greater than or equal to b? {}", a >= b);
    println!("Is a less than or equal to b? {}", a <= b);
}

Pro Tip: These operators return true or false (a.k.a. Boolean values). Perfect for decision-making! 

3. Logical Operators 

Logic is key in programming! Rust provides logical operators to build powerful conditions:

fn main() {
    let is_rust_fun = true;
    let is_sleep_important = false;
    
    println!("AND: {}", is_rust_fun && is_sleep_important); // Logical AND
    println!("OR: {}", is_rust_fun || is_sleep_important);  // Logical OR
    println!("NOT: {}", !is_rust_fun);                      // Logical NOT
}

Fun Fact: Rust doesn't allow non-Boolean values in conditions (e.g., if 5 {} won't work). Always use true or false!

4. Bitwise Operators 

Rust also provides bitwise operators for low-level magic. If you're into binary manipulation, check these out:

fn main() {
    let a = 0b1100; // 12 in binary
    let b = 0b1010; // 10 in binary
    
    println!("Bitwise AND: {:04b}", a & b);  // 1000
    println!("Bitwise OR: {:04b}", a | b);   // 1110
    println!("Bitwise XOR: {:04b}", a ^ b);  // 0110
    println!("Bitwise NOT: {:04b}", !a & 0b1111); // 0011
    println!("Left Shift: {:04b}", a << 1);  // 11000
    println!("Right Shift: {:04b}", a >> 1); // 0110
}

 Why use this? Bitwise operations are super fast and useful for optimizing performance in system-level programming! 

5. Assignment Operators 

Rust supports compound assignment operators, which are shortcuts for updating variables:

fn main() {
    let mut x = 10;
    x += 5; // Same as x = x + 5;
    x -= 3; // Same as x = x - 3;
    x *= 2; // Same as x = x * 2;
    x /= 4; // Same as x = x / 4;
    x %= 3; // Same as x = x % 3;
    
    println!("Final x: {}", x);
}

Pro Tip: Using these shortcuts makes your code cleaner and easier to read! 📖

Wrapping Up 

Rust gives you all the operators you need to handle numbers, logic, and even bits like a pro. Mastering these will make your Rust coding journey smoother and more fun. Now go forth and Rustify your code! 

Post a Comment

0 Comments