Skip to main content
Rust Programming
CHAPTER 05 Beginner

Operators and Expressions in Rust

Updated: May 18, 2026
5 min read

# CHAPTER 5

Operators and Expressions in Rust

1. Chapter Introduction

Programming is fundamentally about taking data, performing calculations or logic on it, and returning a result. In this chapter, we will cover the basic mathematical and logical operators in Rust. More importantly, we will cover one of the most defining characteristics of Rust: the distinction between Statements and Expressions. Understanding this early will save you a lot of confusion when writing functions later!

2. Learning Objectives

By the end of this chapter, you will be able to:
  • Perform basic math using Arithmetic Operators.
  • Compare values using Comparison Operators.
  • Chain logic using Logical Operators.
  • Understand what a Statement is (does not return a value).
  • Understand what an Expression is (returns a value).

3. Arithmetic Operators

Rust supports the standard mathematical operators. Note that you cannot perform math on mismatched data types (e.g., adding an i32 to an f64).
rust
1234567
fn main() {
    let sum = 5 + 10;          // Addition
    let difference = 95.5 - 4.3; // Subtraction
    let product = 4 * 30;      // Multiplication
    let quotient = 56.0 / 32.2;  // Division
    let remainder = 43 % 5;    // Modulo (Returns the remainder, which is 3)
}

4. Comparison Operators

Comparison operators evaluate two values and return a bool (true or false).
rust
123456789
fn main() {
    let a = 10;
    let b = 20;

    println!("Is a equal to b? {}", a == b);   // false
    println!("Is a not equal to b? {}", a != b); // true
    println!("Is a greater than b? {}", a > b);  // false
    println!("Is a less than or equal? {}", a <= b); // true
}

5. Logical Operators

Logical operators are used to combine multiple boolean expressions.
  • && (Logical AND): True if BOTH sides are true.
  • || (Logical OR): True if AT LEAST ONE side is true.
  • ! (Logical NOT): Reverses the boolean value.
rust
12345678910
fn main() {
    let is_adult = true;
    let has_ticket = false;

    // Both must be true
    let can_enter = is_adult && has_ticket; // false
    
    // Reverse the value
    let is_minor = !is_adult; // false
}

6. Statements vs Expressions (CRITICAL CONCEPT)

Rust is an expression-based language. Most things in Rust return a value. To master Rust, you must understand the difference:

A. Statements Statements are instructions that perform some action but do not return a value.

  • Variable declarations are statements: let y = 6;
  • Function definitions are statements: fn main() {}
  • Because let y = 6; does not return a value, you cannot do this in Rust: let x = (let y = 6); (This works in C, but causes an error in Rust).

B. Expressions Expressions evaluate to a resulting value.

  • Math is an expression: 5 + 6 evaluates to 11.
  • Calling a function is an expression.
  • A new scope block {} is an expression!

Look closely at this example:

rust
1234567891011121314151617
fn main() {
    let x = 5;

    // This entire block is an expression.
    // It calculates a value and assigns it to 'y'
    let y = {
        let x_squared = x * x;
        let x_cubed = x_squared * x;
        
        // Notice there is NO SEMICOLON at the end of this line!
        // Expressions do not include ending semicolons. 
        // If you add a semicolon, it becomes a statement and returns nothing '()'.
        x_cubed + 1 
    };

    println!("The value of y is: {}", y); // Output: 126
}

7. Common Mistakes

  • Type Mismatch in Math: Attempting let sum = 5 + 3.2;. The compiler will panic. You must explicitly cast the integer to a float using the as keyword: let sum = 5 as f64 + 3.2;.
  • Accidental Semicolons in Expressions: If you write xcubed + 1; with a semicolon inside a block assigned to a variable, the block will return an empty tuple () instead of the integer value.

8. Best Practices

  • Use Expressions for clean code: Whenever possible, use blocks {} to calculate and return values directly into variables, rather than declaring a mutable variable and updating it line by line.

9. Exercises

  1. 1. Create two variables, price and taxrate. Calculate the total_cost.
  1. 2. Write an expression block {} that calculates the area of a circle ($Area = 3.14 * r * r$) and returns it to a variable area. Remember not to use a semicolon on the last line!

10. MCQs with Answers

Question 1

What operator is used to find the remainder of a division?

Question 2

Which operator represents Logical AND?

Question 3

Does the statement let x = 5; return a value?

Question 4

What is an Expression in Rust?

Q5. Is a scope block {} considered an expression in Rust? a) Yes b) No Answer: a) Yes.
Question 6

What happens if you add a semicolon ; to the very last line of an expression block?

Q7. Will let a = 5 + 5.5; compile? a) Yes b) No, you cannot add an integer and a float without explicit conversion Answer: b) No, types must match.
Question 8

How do you cast an integer to a float?

Question 9

What does the ! operator do?

Q10. Can you assign multiple variables at once like let x = (let y = 6);? a) Yes b) No, because let y = 6 is a statement and does not return a value Answer: b) No.

11. Interview Questions

  • Q: Explain the difference between a Statement and an Expression in Rust.
  • Q: Why does adding a semicolon to the end of a block change its behavior?

12. FAQs

  • Why are there no increment operators (++) in Rust? Rust does not have x++ or x-- because they can lead to subtle bugs regarding order of evaluation (e.g., x++ vs ++x). Instead, explicitly use x += 1.

13. Summary

Operators in Rust behave exactly as they do in C or Java. However, the strict separation between Statements (actions) and Expressions (evaluations) is unique. By omitting the trailing semicolon, any block of code can become an expression that seamlessly returns data, leading to concise and powerful code.

14. Next Chapter Recommendation

Now that we can manipulate data, let's learn how to make our programs interactive. In Chapter 6: User Input and Output, we will learn how to read data typed into the terminal by the user and process it securely.

Finish this Chapter

Save your progress on your learning path and prepare for coding interview challenges.

Discussion

Join the discussion

Log in or create a free account to participate.

Sort: ·