मुख्य कंटेंट तक स्किप करें

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.

💬 Discuss this page

Have a question or spot something confusing in "Rust Operators"? Ask below — it's backed by GitHub Discussions, so maintainers get notified like any other GitHub activity.