Home Rust Tutorial: Operators in Rust Programming Language

Tutorial: Operators in Rust Programming Language

Operators are fundamental in programming and are used to perform various computations, comparisons, and logical operations.

Rust provides a rich set of operators, categorized into arithmetic, comparison, logical, bitwise, assignment, and more.

This tutorial covers:

1. Arithmetic Operators

Rust supports the basic arithmetic operators:

Operator Description Example
+ Addition a + b
Subtraction a – b
* Multiplication a * b
/ Division a / b
% Modulus (Remainder) a % b

Example: Arithmetic Operations

fn main() {
    let a = 10;
    let b = 3;

    println!("a + b = {}", a + b);  // Addition
    println!("a - b = {}", a - b);  // Subtraction
    println!("a * b = {}", a * b);  // Multiplication
    println!("a / b = {}", a / b);  // Division
    println!("a % b = {}", a % b);  // Modulus
}

2. Comparison Operators

Comparison operators return a boolean value (true or false).

Operator Description Example
== Equal to a == b
!= Not equal to a != b
> Greater than a > b
< Less than a < b
>= Greater than or equal to a >= b
<= Less than or equal to a <= b

Example: Comparison Operations

fn main() {
    let x = 10;
    let y = 20;

    println!("x == y: {}", x == y);  // Equal to
    println!("x != y: {}", x != y);  // Not equal to
    println!("x > y: {}", x > y);    // Greater than
    println!("x < y: {}", x < y);    // Less than
    println!("x >= y: {}", x >= y);  // Greater than or equal to
    println!("x <= y: {}", x <= y);  // Less than or equal to
}

3. Logical Operators

Logical operators are used to combine or invert boolean values.

Operator Description Example
&& Logical AND a && b
` `
! Logical NOT !a

Example: Logical Operations

fn main() {
    let a = true;
    let b = false;

    println!("a && b: {}", a && b);  // Logical AND
    println!("a || b: {}", a || b);  // Logical OR
    println!("!a: {}", !a);          // Logical NOT
}

4. Bitwise Operators

Bitwise operators operate on binary representations of numbers.

Operator Description Example
& Bitwise AND a & b
` ` Bitwise OR
^ Bitwise XOR a ^ b
! Bitwise NOT !a
<< Left shift a << b
>> Right shift a >> b

Example: Bitwise Operations

fn main() {
    let a = 5;  // 0101 in binary
    let b = 3;  // 0011 in binary

    println!("a & b = {}", a & b);  // Bitwise AND
    println!("a | b = {}", a | b);  // Bitwise OR
    println!("a ^ b = {}", a ^ b);  // Bitwise XOR
    println!("!a = {}", !a);        // Bitwise NOT
    println!("a << 1 = {}", a << 1);  // Left shift
    println!("a >> 1 = {}", a >> 1);  // Right shift
}

5. Compound Assignment Operators

Compound operators combine arithmetic or bitwise operations with assignment.

Operator Description Example
+= Add and assign a += b
-= Subtract and assign a -= b
*= Multiply and assign a *= b
/= Divide and assign a /= b
%= Modulus and assign a %= b
&= Bitwise AND and assign a &= b
` =` Bitwise OR and assign
^= Bitwise XOR and assign a ^= b
<<= Left shift and assign a <<= b
>>= Right shift and assign a >>= b

Example: Compound Assignment

fn main() {
    let mut a = 10;

    a += 5;  // a = a + 5
    println!("a after += 5: {}", a);

    a *= 2;  // a = a * 2
    println!("a after *= 2: {}", a);

    a %= 3;  // a = a % 3
    println!("a after %= 3: {}", a);
}

6. Range Operators

Rust provides operators to define ranges, commonly used in loops.

Operator Description Example
.. Exclusive range 0..5
..= Inclusive range 0..=5

Example: Range Operators

fn main() {
    for i in 0..5 {  // 0 to 4
        println!("Exclusive range: {}", i);
    }

    for i in 0..=5 {  // 0 to 5
        println!("Inclusive range: {}", i);
    }
}

7. Miscellaneous Operators

7.1 Dereference Operator

Access the value at a memory address.

fn main() {
    let x = 10;
    let y = &x;  // Reference to x
    println!("Dereference: {}", *y);  // Dereference y
}

7.2 Ternary-like if Expression

Rust doesn’t have a ternary operator, but if can act similarly.

fn main() {
    let x = 10;
    let y = if x > 5 { "Greater" } else { "Smaller" };
    println!("y: {}", y);
}

8. Practical Examples

8.1 Calculating Factorials

Using a range operator and compound assignment.

fn main() {
    let mut result = 1;
    for i in 1..=5 {
        result *= i;
    }
    println!("5! = {}", result);
}

8.2 Checking Bit Flags

Use bitwise operators to work with flags.

fn main() {
    let read = 0b0001; // 1
    let write = 0b0010; // 2
    let execute = 0b0100; // 4

    let permissions = read | write;

    println!("Can read: {}", (permissions & read) != 0);
    println!("Can write: {}", (permissions & write) != 0);
    println!("Can execute: {}", (permissions & execute) != 0);
}

Key Takeaways

  • Rust offers a wide range of operators for arithmetic, comparison, logical, bitwise, and compound operations.
  • Operators are designed to work seamlessly with Rust's safety and performance guarantees.
  • Range operators (.., ..=) are especially useful in loops and sequences.
  • Rust's explicit type system and strict compile-time checks make operator usage safe and predictable.

Experiment with these examples to fully grasp Rust's operator capabilities!

 

You may also like