techmore.in

Rust - Operators

Rust provides a variety of operators for performing operations on data. These operators include arithmetic, comparison, logical, bitwise, and others. Here's a breakdown of the different types of operators in Rust:

1. Arithmetic Operators

Arithmetic operators perform basic mathematical operations.

Operator Description Example Result
+ Addition 5 + 3 8
- Subtraction 5 - 3 2
* Multiplication 5 * 3 15
/ Division 6 / 3 2
% Remainder (modulus) 5 % 3 2

Example:

rust
fn main() { let sum = 5 + 3; let difference = 5 - 3; let product = 5 * 3; let quotient = 6 / 3; let remainder = 5 % 3; println!("Sum: {}", sum); println!("Difference: {}", difference); println!("Product: {}", product); println!("Quotient: {}", quotient); println!("Remainder: {}", remainder); }

2. Comparison Operators

Comparison operators compare two values and return a boolean (true or false).

Operator Description Example Result
== Equal to 5 == 3 false
!= Not equal to 5 != 3 true
> Greater than 5 > 3 true
< Less than 5 < 3 false
>= Greater or equal 5 >= 3 true
<= Less or equal 5 <= 3 false

Example:

rust
fn main() { let is_equal = 5 == 3; let is_not_equal = 5 != 3; let is_greater = 5 > 3; let is_lesser = 5 < 3; let is_greater_equal = 5 >= 3; let is_lesser_equal = 5 <= 3; println!("Is equal: {}", is_equal); println!("Is not equal: {}", is_not_equal); println!("Is greater: {}", is_greater); println!("Is lesser: {}", is_lesser); println!("Is greater or equal: {}", is_greater_equal); println!("Is lesser or equal: {}", is_lesser_equal); }

3. Logical Operators

Logical operators are used to perform logical operations on boolean values.

Operator Description Example Result
&& Logical AND true && false false
` ` Logical OR
! Logical NOT !true false

Example:

rust
fn main() { let and_result = true && false; let or_result = true || false; let not_result = !true; println!("AND result: {}", and_result); println!("OR result: {}", or_result); println!("NOT result: {}", not_result); }

4. Bitwise Operators

Bitwise operators perform operations on the binary representations of integers.

Operator Description Example Result
& Bitwise AND 0b1100 & 0b1010 0b1000
` ` Bitwise OR 0b1100 | 0b1010
^ Bitwise XOR 0b1100 ^ 0b1010 0b0110
! Bitwise NOT !0b1100 0b0011
<< Left shift 0b0001 << 2 0b0100
>> Right shift 0b0100 >> 2 0b0001

Example:

rust
fn main() { let and_result = 0b1100 & 0b1010; let or_result = 0b1100 | 0b1010; let xor_result = 0b1100 ^ 0b1010; let not_result = !0b1100 & 0b1111; let left_shift = 0b0001 << 2; let right_shift = 0b0100 >> 2; println!("AND result: {:04b}", and_result); println!("OR result: {:04b}", or_result); println!("XOR result: {:04b}", xor_result); println!("NOT result: {:04b}", not_result); println!("Left shift result: {:04b}", left_shift); println!("Right shift result: {:04b}", right_shift); }

5. Assignment Operators

Assignment operators assign a value to a variable and can perform operations simultaneously.

Operator Description Example Equivalent To
= Assign x = 5 x = 5
+= Add and assign x += 3 x = x + 3
-= Subtract and assign x -= 3 x = x - 3
*= Multiply and assign x *= 3 x = x * 3
/= Divide and assign x /= 3 x = x / 3
%= Modulus and assign x %= 3 x = x % 3
&= Bitwise AND and assign x &= 3 x = x & 3
` =` Bitwise OR and assign `x
^= Bitwise XOR and assign x ^= 3 x = x ^ 3
<<= Left shift and assign x <<= 3 x = x << 3
>>= Right shift and assign x >>= 3 x = x >> 3

Example:

rust
fn main() { let mut x = 5; x += 3; println!("x after +=: {}", x); // Output: 8 x -= 2; println!("x after -=: {}", x); // Output: 6 x *= 2; println!("x after *=: {}", x); // Output: 12 x /= 4; println!("x after /=: {}", x); // Output: 3 x %= 2; println!("x after %=: {}", x); // Output: 1 }

6. Other Operators

  • Range Operators (.., ..=): Used to create ranges.

    rust
    fn main() { for i in 1..5 { // Excludes 5 println!("{}", i); } for i in 1..=5 { // Includes 5 println!("{}", i); } }
  • Dereference Operator (*): Used to dereference a pointer to access the value it points to.

    rust
    fn main() { let x = 5; let y = &x; println!("Dereferenced y: {}", *y); // Output: 5 }
  • Reference Operator (&): Used to create a reference to a value.

    rust
    fn main() { let x = 5; let y = &x; println!("y points to: {}", y); // Output: y points to: 5 }
  • Type Cast (as): Used to convert one type to another.

    rust
    fn main() { let x = 5.5; let y = x as i32; println!("y: {}", y); // Output: y: 5 }

Summary

  • Arithmetic operators: +, -, *, /, %
  • Comparison operators: ==, !=, >, <, >=, <=
  • Logical operators: &&, ||, !
  • Bitwise operators: &, |, ^, !, <<, >>
  • Assignment operators: =, +=, -=, *=, /=, %=, &=, |=, ^=, <<=, >>=
  • Range operators: .., ..=
  • Dereference and reference operators: *, &
  • Type casting: as

Understanding these operators helps in effectively performing operations on data in Rust, making your code more efficient and readable.