Rust uses if, else if, and else for branching logic, just like JavaScript. It also provides a powerful match expression for pattern matching. Both are used to control the flow of your program based on conditions or value patterns.
In most programs, you need to make decisions: check if a number is positive, compare values, or match a user input. Rust gives you two main tools to do this: if and match.
if works in a similar way to other languages. It evaluates a condition and runs code only if that condition is true. The match expression is unique to Rust. It allows you to compare a value against multiple patterns, making it clearer and safer for many decision structures.
If you have not read about Operators in Rust yet, it will help you understand the conditions used in these decisions.
Using if Statements
Here is the basic form of an if statement in Rust:
let age = 18;
if age >= 18 {
println!("You can vote.");
} else {
println!("You are too young.");
}
You can also use else if to check multiple conditions:
let score = 85;
if score >= 90 {
println!("Grade: A");
} else if score >= 80 {
println!("Grade: B");
} else {
println!("Grade: C");
}
Each if condition must be a bool. You cannot use other types, even if they seem truthy. For example:
let number = 5;
if number {
// This will not compile
}
Rust will show a compile error. You must write:
if number != 0 {
// This is valid
}
Using if as an Expression
In Rust, if is not just a control statement. It is also an expression, which means it can return a value.
let condition = true;
let number = if condition { 10 } else { 5 };
This lets you assign different values based on a condition. All branches must return the same type, or the compiler will show an error.
Introducing match
The match expression is one of the most powerful features in Rust. It works like a switch in other languages, but it is more strict and safe.
Example:
let number = 3;
match number {
1 => println!("One"),
2 => println!("Two"),
3 => println!("Three"),
_ => println!("Something else"),
}
Each => maps a pattern to an action. The _ is a wildcard that matches anything not listed above.
Pattern Matching with match
You can use match to check values of enums, Option, and even string values:
let day = "Monday";
match day {
"Monday" => println!("Start of the week"),
"Friday" => println!("Almost weekend"),
"Sunday" => println!("Rest day"),
_ => println!("Just another day"),
}
Another example using Option:
let name: Option<&str> = Some("Alice");
match name {
Some(n) => println!("Name is {}", n),
None => println!("No name provided"),
}
This is much safer than checking null or undefined, and it forces you to handle all possible cases.
match vs if
Use if for simple true/false conditions. Use match when you have multiple cases to check or want to handle different data patterns.
For example, if is better here:
if age >= 18 {
println!("Adult");
}
But match is better here:
match role {
"admin" => println!("Full access"),
"user" => println!("Limited access"),
_ => println!("Guest access"),
}
Summary
Rust uses if, else if, and else to handle conditions. It also has match for pattern matching, which helps you handle multiple cases clearly. Both are expressions, meaning they can return values. Match is especially powerful when used with enums like Option or Result.
In the next post, I will explain Loops in Rust. You will learn how to repeat code using for, while, and loop, and how to control them with break and continue.
Next up: Loops in Rust