What Are Rust Operators?

In Rust, operators are special symbols or keywords that perform operations on values or variables. We can do multiple operations like adding numbers, comparing values, or applying logical conditions.

Rust operators is important for beginners because most of the programs working with operators logics.

Learn these topics about operators:

  • Why Are Operators Important in Rust?
  • Types of Rust Operators
  • Beginner-friendly examples for each operator

Why Are Operators Important in Rust?

Operators make our code shorter and faster, because if you write long expressions or functions, so code will be heavy and take time to run. Instead, we will use symbols like +, ==, or && to achieve the same result with less code.

Rust and other languages use similar operators, but this language adds safety features like strong typing and ownership checks to prevent runtime errors.

Types of Rust Operators

  • Arithmetic Operators
  • Comparison Operators
  • Logical Operators
  • Assignment Operators
  • Bitwise Operators
  • Range and Other Special Operators

1) Arithmetic Operators

Arithmetic operators perform basic mathematical operations.

  • + (Addition)
  • - (Subtraction)
  • * (Multiplication)
  • / (Division)
  • % (Remainder)

Learn this simple examples of each calculation:

fn main() {
let price: i32 = 50;
let tax: i32 = 5;

let total = price + tax; // Addition
let discount = total - 10; // Subtraction
let doubled = discount * 2;
let per_item = doubled / 3;
let remainder = doubled % 3;

println!("Total: {}", total);
println!("After discount: {}", discount);
println!("Doubled: {}", doubled);
println!("Per item: {}", per_item);
println!("Remainder: {}", remainder);
}

This example calculates a simple bill with performs multiple arithmetic operations.

2) Comparison Operators

These operators are used to compare two values and return a result in Boolean (true or false). It has multiple comparison symbols like:

  • == (Equal to)
  • != (Not equal to)
  • > (Greater than)
  • < (Less than)
  • >= (Greater than or equal to)
  • <= (Less than or equal to)

See this example that performs each compare operation:

fn main() {
let score = 75;

println!("Passed exam? {}", score >= 50);
println!("Perfect score? {}", score == 100);
println!("Needs retake? {}", score < 40);
}

This program checks whether a student passed, got a perfect score, or needs a retake.

3) Logical Operators

This operator is used to combine multiple conditions in our programs.

  • && (Logical AND) – true if both conditions are true
  • | | (Logical OR) – true if at least one condition is true
  • ! (Logical NOT) – inverts a boolean value

For example:

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

if age >= 18 && has_id {
println!("Entry allowed.");
} else {
println!("Entry denied.");
}

println!("Is minor? {}", !(age >= 18));
}

This code checks entry permission by combining values using && and !.

4) Assignment Operators

These operators combine assignment with an operation. It has multiple signs like:

  • = (Assign)
  • +=, -=, *=, /=, %= (Update and assign)

For example:

fn main() {
let mut balance = 100;

balance += 50; // Add money
balance -= 20; // Spend money
balance *= 2; // Double it
balance /= 5; // Divide
balance %= 3; // Get remainder

println!("Final balance: {}", balance);
}

5) Bitwise Operators

Rust also supports operators for bit-level operations, which are useful in systems programming and optimisation.

  • & (Bitwise AND)
  • | (Bitwise OR)
  • ^ (Bitwise XOR)
  • << (Left shift)
  • >> (Right shift)

Example:

fn main() {
let a: u8 = 6; // 00000110
let b: u8 = 3; // 00000011

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

6) Range and Other Special Operators

This operator is especially useful in loops and slices. See this below:

  • start..end (Excludes end)
  • start..=end (Includes end)
fn main() {
for i in 1..=5 {
println!("Counting: {}", i);
}
} /// This program counts 1 to 5.

Practice Exercise of Operators

Exercise 1: Calculate Student Total and Average

  1. Create three immutable variables to store marks of three subjects (78, 85, 92).
  2. Use arithmetic operators to:
    • Calculate the total marks.
    • Calculate the average marks.
  3. Print both results as output.

Expected Output of this program:

Total Marks: 255
Average Marks: 85

Exercise 2: Age Eligibility Checker

  1. Create two variables: age (16) and has_id (boolean, true).
  2. Use comparison and logical operators to check:
    • If the person is 18 or older and has an ID, print “Eligible for entry”.
    • Otherwise, print “Not eligible”.

Expected Output of this program:

Not eligible

Leave a Comment