Conditional Statements in Rust
In this lesson, you will learn how to make decisions in Rust using conditional statements. Conditional statements allow a program to execute different blocks of code based on conditions.
Rust provides powerful and safe conditional constructs that help control program flow clearly.
What Is a Conditional Statement?
A conditional statement checks whether a condition is true or false
and executes code accordingly.
Conditions in Rust must always evaluate to a boolean value.
The if Statement
The if statement executes a block of code only when a condition is true.
Unlike some languages, Rust does not allow non-boolean conditions.
fn main() {
let number = 10;
if number > 5 {
println!("Number is greater than 5");
}
}
If the condition evaluates to false, the code inside the block is skipped.
The if-else Statement
The if-else statement allows you to execute one block when a condition is true
and another block when it is false.
fn main() {
let age = 16;
if age >= 18 {
println!("You are eligible to vote");
} else {
println!("You are not eligible to vote");
}
}
Only one block of code will execute.
The else if Ladder
When multiple conditions need to be checked, you can use else if.
Rust evaluates conditions from top to bottom and stops at the first match.
fn main() {
let score = 82;
if score >= 90 {
println!("Grade: A");
} else if score >= 75 {
println!("Grade: B");
} else if score >= 60 {
println!("Grade: C");
} else {
println!("Grade: F");
}
}
Using if as an Expression
In Rust, if is an expression, which means it can return a value.
Both branches must return values of the same type.
fn main() {
let number = 7;
let result = if number % 2 == 0 {
"Even"
} else {
"Odd"
};
println!("{}", result);
}
This makes Rust code more concise and expressive.
Nested if Statements
You can place one if statement inside another.
Nested conditions should be used carefully to maintain readability.
fn main() {
let age = 25;
let has_id = true;
if age >= 18 {
if has_id {
println!("Entry allowed");
} else {
println!("ID required");
}
} else {
println!("Underage");
}
}
Logical Operators in Conditions
Conditions can be combined using logical operators such as && and ||.
fn main() {
let age = 20;
let citizen = true;
if age >= 18 && citizen {
println!("Eligible to apply");
}
}
Common Mistakes
- Using non-boolean values in conditions
- Forgetting braces
{}around blocks - Returning different types from
ifexpressions
📝 Practice Exercises
Exercise 1
Write an if statement to check if a number is positive.
Exercise 2
Use if-else to check whether a person can vote.
Exercise 3
Use else if to assign grades based on marks.
Exercise 4
Use an if expression to assign a value to a variable.
✅ Practice Answers
Answer 1
fn main() {
let n = 5;
if n > 0 {
println!("Positive number");
}
}
Answer 2
fn main() {
let age = 17;
if age >= 18 {
println!("Can vote");
} else {
println!("Cannot vote");
}
}
Answer 3
fn main() {
let marks = 70;
if marks >= 80 {
println!("A");
} else if marks >= 60 {
println!("B");
} else {
println!("C");
}
}
Answer 4
fn main() {
let x = 4;
let result = if x % 2 == 0 { "Even" } else { "Odd" };
println!("{}", result);
}
What’s Next?
Now that you understand conditional statements, you are ready to learn how to repeat actions using loops.
In the next lesson, you will explore loops in Rust.