Rust Control Flow — If/Else, Loops (loop, while, for), and Match Statements
DodaTech
Updated 2026-06-28
1 min read
In this tutorial, you will learn about Rust Control Flow. We cover key concepts, practical examples, and best practices to help you master this topic.
Rust control flow includes if/else expressions, loop with break/continue, while with conditions, for over iterators, and match for pattern matching.
What You'll Learn
- if/else expressions
- loop, while, for
- Match with patterns
- Loop labels
Why It Matters
Control flow structures all Rust programs. Firefox uses match extensively for HTTP Parsing. DodaZIP uses for loops for file iteration and match for file type detection.
Real-World Use
State machines, HTTP routing, data validation, iterative processing.
fn main() {
// if/else expression
let condition = true;
let number = if condition { 5 } else { 6 };
// loop with break
let mut counter = 0;
let result = loop {
counter += 1;
if counter == 10 { break counter * 2; }
};
// while
let mut n = 3;
while n > 0 { println!("{}", n); n -= 1; }
// for
for i in 0..5 { println!("{}", i); }
// match
let value = 3;
match value {
1 => println!("one"),
2 | 3 => println!("two or three"),
4..=10 => println!("range"),
_ => println!("other"),
}
println!("Result: {}", result);
}
← Previous
Rust Functions — Function Definitions, Parameters, Return Values, and Expressions
Next →
Rust Ownership — The Ownership Model, Move Semantics, and Copy Types
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro