Start Coding

Rust Operators

Operators in Rust are symbols that perform specific operations on one, two, or three operands. They are essential for manipulating data and controlling program flow. Rust provides a variety of operators, including arithmetic, comparison, logical, and bitwise operators.

Arithmetic Operators

Arithmetic operators perform mathematical calculations on numeric values. Rust supports the following arithmetic operators:

  • + (addition)
  • - (subtraction)
  • * (multiplication)
  • / (division)
  • % (modulus)

Example of arithmetic operators in action:


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!("Modulus: {}", a % b);
}
    

Comparison Operators

Comparison operators are used to compare two values and return a boolean result. Rust provides the following comparison operators:

  • == (equal to)
  • != (not equal to)
  • < (less than)
  • > (greater than)
  • <= (less than or equal to)
  • >= (greater than or equal to)

Logical Operators

Logical operators are used to combine boolean expressions. Rust supports the following logical operators:

  • && (logical AND)
  • || (logical OR)
  • ! (logical NOT)

Example of comparison and logical operators:


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

    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);

    let a = true;
    let b = false;

    println!("a && b: {}", a && b);
    println!("a || b: {}", a || b);
    println!("!a: {}", !a);
}
    

Bitwise Operators

Bitwise operators manipulate individual bits of integer values. Rust provides the following bitwise operators:

  • & (bitwise AND)
  • | (bitwise OR)
  • ^ (bitwise XOR)
  • << (left shift)
  • >> (right shift)

Assignment Operators

Assignment operators are used to assign values to variables. Rust supports compound assignment operators that combine arithmetic or bitwise operations with assignment:

  • +=
  • -=
  • *=
  • /=
  • %=
  • &=
  • |=
  • ^=
  • <<=
  • >>=

Operator Precedence

Rust follows a specific order of precedence for operators. When multiple operators are used in an expression, the operators with higher precedence are evaluated first. Parentheses can be used to override the default precedence.

Type-specific Operators

Rust also provides some type-specific operators:

  • as (type casting)
  • & (borrow)
  • &mut (mutable borrow)
  • * (dereference)

These operators are crucial for working with Rust's Ownership Concept and References.

Conclusion

Understanding Rust operators is essential for writing efficient and expressive code. They allow you to perform calculations, make comparisons, and manipulate data in various ways. As you continue your Rust journey, you'll find these operators indispensable in your programming toolkit.

For more information on related topics, check out Rust Control Flow and Rust Data Types.