codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
🦀

Rust

7 / 58 topics
1Getting Started with Rust2Installing Rust3Hello World Program4Rust Syntax5Variables and Mutability6Data Types7Operators
Tutorials/Rust/Operators
🦀Rust

Operators

Updated 2026-05-15
10 min read

Operators

Introduction

In programming, operators are symbols or keywords that perform operations on variables and values. Rust, like many other programming languages, provides a variety of operators to handle arithmetic, comparison, and logical operations. Understanding these operators is fundamental to writing effective and efficient Rust code.

Concept

Arithmetic Operators

Arithmetic operators are used to perform basic mathematical operations such as addition, subtraction, multiplication, division, and modulus.

  • Addition (+): Adds two operands.
  • Subtraction (-): Subtracts the second operand from the first.
  • Multiplication (*): Multiplies two operands.
  • Division (/): Divides the first operand by the second. Note that division of integers truncates the result towards zero.
  • Modulus (%): Returns the remainder of the division of the first operand by the second.

Comparison Operators

Comparison operators are used to compare two values and return a boolean result (true or false).

  • Equal to (==): Checks if the two operands are equal.
  • Not equal to (!=): Checks if the two operands are not equal.
  • Greater than (>): Checks if the first operand is greater than the second.
  • Less than (<): Checks if the first operand is less than the second.
  • Greater than or equal to (>=): Checks if the first operand is greater than or equal to the second.
  • Less than or equal to (<=): Checks if the first operand is less than or equal to the second.

Logical Operators

Logical operators are used to combine multiple conditions and return a boolean result.

  • Logical AND (&&): Returns true if both operands are true.
  • Logical OR (||): Returns true if at least one of the operands is true.
  • Logical NOT (!): Inverts the boolean value of its operand. If the operand is true, it returns false, and vice versa.

Examples

Let's explore each type of operator with practical examples.

Arithmetic Operators

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

    println!("Addition: {} + {} = {}", a, b, a + b); // OutputBlock{`Addition: 10 + 3 = 13`}
    println!("Subtraction: {} - {} = {}", a, b, a - b); // OutputBlock{`Subtraction: 10 - 3 = 7`}
    println!("Multiplication: {} * {} = {}", a, b, a * b); // OutputBlock{`Multiplication: 10 * 3 = 30`}
    println!("Division: {} / {} = {}", a, b, a / b); // OutputBlock{`Division: 10 / 3 = 3`}
    println!("Modulus: {} % {} = {}", a, b, a % b); // OutputBlock{`Modulus: 10 % 3 = 1`}
}

### Comparison Operators

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

    println!("x == y: {}", x == y); // OutputBlock{`x == y: false`}
    println!("x != y: {}", x != y); // OutputBlock{`x != y: true`}
    println!("x > y: {}", x > y); // OutputBlock{`x > y: false`}
    println!("x < y: {}", x < y); // OutputBlock{`x < y: true`}
    println!("x >= y: {}", x >= y); // OutputBlock{`x >= y: false`}
    println!("x <= y: {}", x <= y); // OutputBlock{`x <= y: true`}
}

### Logical Operators

```rust
fn main() {
    let p = true;
    let q = false;

    println!("p && q: {}", p && q); // OutputBlock{`p && q: false`}
    println!("p || q: {}", p || q); // OutputBlock{`p || q: true`}
    println!("!p: {}", !p); // OutputBlock{`!p: false`}
    println!("!q: {}", !q); // OutputBlock{`!q: true`}
}

## What's Next?

In the next section, we will explore control flow in Rust, which includes conditional statements and loops. Understanding how to control the flow of your program is crucial for building more complex applications.

Stay tuned!

PreviousData TypesNext Control Flow

Recommended Gear

Data TypesControl Flow