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 ani32 to an f64).
rust
4. Comparison Operators
Comparison operators evaluate two values and return abool (true or false).
rust
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
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 + 6evaluates to11.
- Calling a function is an expression.
-
A new scope block
{}is an expression!
Look closely at this example:
rust
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 theaskeyword: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.
Create two variables,
priceandtaxrate. Calculate thetotal_cost.
-
2.
Write an expression block
{}that calculates the area of a circle ($Area = 3.14 * r * r$) and returns it to a variablearea. 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?
{} 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?
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?
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 havex++orx--because they can lead to subtle bugs regarding order of evaluation (e.g.,x++vs++x). Instead, explicitly usex += 1.