Skip to main content

Rust Operators

Ayesha
EditReport

Rust Operators

Operators are symbols used to perform operations on variables and values.

Rust provides different types of operators such as:

  1. Arithmetic Operators
  2. Assignment Operators
  3. Comparison Operators
  4. Logical Operators
  5. Bitwise Operators

Video Explanationโ€‹


1. Arithmetic Operators

Arithmetic operators are used to perform mathematical calculations.

OperatorDescriptionExample
+Additiona + b
-Subtractiona - b
*Multiplicationa * b
/Divisiona / b
%Modulusa % b

Exampleโ€‹

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

Outputโ€‹

Addition: 13
Subtraction: 7
Multiplication: 30
Division: 3
Modulus: 1

2. Assignment Operators

Assignment operators are used to assign values to variables.

OperatorExampleMeaning
=x = 5Assign value
+=x += 2Add and assign
-=x -= 2Subtract and assign
*=x *= 2Multiply and assign
/=x /= 2Divide and assign

Exampleโ€‹

fn main() {
let mut x = 10;

x += 5;
println!("x = {}", x);

x -= 2;
println!("x = {}", x);

x *= 3;
println!("x = {}", x);
}

Outputโ€‹

x = 15
x = 13
x = 39

3. Comparison Operators

Comparison operators compare two values and return true or false.

OperatorDescription
==Equal to
!=Not equal to
>Greater than
<Less than
>=Greater than or equal to
<=Less than or equal to

Exampleโ€‹

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

println!("{}", a == b);
println!("{}", a != b);
println!("{}", a < b);
println!("{}", a > b);
}

Outputโ€‹

false
true
true
false

4. Logical Operators

Logical operators are used to combine conditions.

OperatorDescription
&&Logical AND
`
!Logical NOT

Exampleโ€‹

fn main() {
let age = 20;
let has_id = true;

println!("{}", age >= 18 && has_id);
println!("{}", age < 18 || has_id);
println!("{}", !has_id);
}

Outputโ€‹

true
true
false

5. Bitwise Operators

Bitwise operators work on binary values.

OperatorDescription
&Bitwise AND
``
^Bitwise XOR
<<Left Shift
>>Right Shift
!Bitwise NOT

Exampleโ€‹

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

println!("AND: {}", a & b);
println!("OR: {}", a | b);
println!("XOR: {}", a ^ b);
}

Outputโ€‹

AND: 1
OR: 7
XOR: 6

Unary Operators

Unary operators work with a single operand.

Exampleโ€‹

fn main() {
let number = 10;

println!("{}", -number);

let status = true;

println!("{}", !status);
}

Outputโ€‹

-10
false

Operator Precedence

Operator precedence determines the order in which operations are performed.

Exampleโ€‹

fn main() {
let result = 10 + 5 * 2;

println!("{}", result);
}

Outputโ€‹

20

In this example:

  • Multiplication happens first
  • Addition happens later

Short-Circuit Operators

Rust uses short-circuit evaluation with logical operators.

Exampleโ€‹

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

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

Outputโ€‹

false
true

Summary

Rust operators are used to perform operations on variables and values.

Types of Operators in Rustโ€‹

  • Arithmetic Operators
  • Assignment Operators
  • Comparison Operators
  • Logical Operators
  • Bitwise Operators
  • Unary Operators

Operators make it easy to perform calculations, comparisons, and logical operations in Rust programs.

Track Your Progress

Done with this topic? Mark it as complete to track your progress.