Rust
December 2, 2026 5 min read

Control Flow in Rust

HS
HEMANT SINGH
Technical Writer & Educator

1. What is Control Flow?

Control flow means the order in which statements and instructions are executed in a program.

Rust provides conditions for decision-making and loops for repeating instructions.

There are two main concepts:

  1. Conditional Statements – Used to make decisions.
  2. Loops – Used to execute code repeatedly.

2. if Statement

The if statement executes a block of code only when a given condition is true.

Syntax

if condition {
// Code executes when condition is true
}

Example

fn main() {
let age = 20;

if age >= 18 {
println!("You are an adult.");
}
}

Output:

You are an adult.

Explanation:

  1. if checks a condition.
  2. age >= 18 is the condition.
  3. If the condition is true, the code inside { } executes.

3. if-else Statement

The else block executes when the if condition is false.

Syntax

if condition {
// If condition is true
} else {
// If condition is false
}

Example

fn main() {
let age = 16;

if age >= 18 {
println!("You can vote.");
} else {
println!("You are not eligible to vote yet.");
}
}

Output:

You are not eligible to vote yet.

4. else-if Statement

Use else if when you need to check multiple conditions.

The first condition that evaluates to true will execute.

Example

fn main() {
let marks = 78;

if marks >= 90 {
println!("Grade A");
} else if marks >= 75 {
println!("Grade B");
} else if marks >= 50 {
println!("Grade C");
} else {
println!("Needs improvement");
}
}

Output:

Grade B

Explanation

  1. First, Rust checks whether marks are greater than or equal to 90.
  2. If false, it checks whether marks are greater than or equal to 75.
  3. The second condition is true, so Grade B is printed.
  4. The remaining conditions are skipped.

5. Nested if Statement

An if statement inside another if statement is called a nested if statement.

Example

fn main() {
let age = 22;
let has_id = true;

if age >= 18 {
if has_id {
println!("Entry allowed.");
} else {
println!("ID is required.");
}
} else {
println!("Entry not allowed.");
}
}

Output:

Entry allowed.

Nested conditions are useful when one condition depends on another condition.

6. if as an Expression

In Rust, if is an expression, which means it can return a value.

Example

fn main() {
let age = 20;

let category = if age >= 18 {
"Adult"
} else {
"Minor"
};

println!("Category: {}", category);
}

Output:

Category: Adult

Important:

  1. Both branches must return compatible data types.
  2. Do not put a semicolon after the final expression when you want that expression to return a value.

7. match Expression

The match expression compares a value against different patterns.

It executes the first matching pattern.

Syntax

match value {
pattern1 => {
// Code
},
pattern2 => {
// Code
},
_ => {
// Default code
},
}

Example

fn main() {
let day = 3;

match day {
1 => println!("Monday"),
2 => println!("Tuesday"),
3 => println!("Wednesday"),
4 => println!("Thursday"),
5 => println!("Friday"),
6 => println!("Saturday"),
7 => println!("Sunday"),
_ => println!("Invalid day"),
}
}

Output:

Wednesday

What is _ in match?

The underscore _ is called a wildcard pattern.

It matches any value not matched by the previous patterns.

Example

fn main() {
let number = 100;

match number {
1 => println!("One"),
2 => println!("Two"),
_ => println!("Other number"),
}
}

Output:

Other number

8. loop Statement

The loop keyword creates an infinite loop.

It continues executing until we explicitly stop it using break.

Syntax

loop {
// Repeated code
}

Example

fn main() {
let mut count = 1;

loop {
println!("Count: {}", count);

count += 1;

if count > 5 {
break;
}
}
}

Output:

Count: 1
Count: 2
Count: 3
Count: 4
Count: 5

Explanation

  1. Initially, count is 1.
  2. The loop prints the current value.
  3. count += 1 increases the value.
  4. When count becomes 6, the break condition becomes true.
  5. The loop stops.

9. while Loop

The while loop executes code as long as its condition remains true.

Unlike loop, it checks the condition before every iteration.

Syntax

while condition {
// Repeated code
}

Example

fn main() {
let mut count = 1;

while count <= 5 {
println!("Count: {}", count);

count += 1;
}
}

Output:

Count: 1
Count: 2
Count: 3
Count: 4
Count: 5

How while Works

  1. First, the condition is checked.
  2. If true, the loop body executes.
  3. The condition is checked again.
  4. If false, the loop terminates.

10. for Loop

The for loop is commonly used to iterate over a range or collection.

Syntax

for variable in range {
// Code
}

Example

fn main() {
for number in 1..=5 {
println!("{}", number);
}
}

Output:

1
2
3
4
5

Understanding Rust Ranges

Rust provides two commonly used range formats.


Range

Meaning

1..5

1, 2, 3, 4

1..=5

1, 2, 3, 4, 5

Important:

  1. .. excludes the ending value.
  2. ..= includes the ending value.

Example: Multiplication Table

fn main() {
let number = 5;

for i in 1..=10 {
println!("{} x {} = {}", number, i, number * i);
}
}

Output:

5 x 1 = 5
5 x 2 = 10
5 x 3 = 15
5 x 4 = 20
5 x 5 = 25
5 x 6 = 30
5 x 7 = 35
5 x 8 = 40
5 x 9 = 45
5 x 10 = 50

11. break Statement

The break statement immediately terminates the current loop.

Example

fn main() {
for number in 1..=10 {
if number == 5 {
break;
}

println!("{}", number);
}
}

Output:

1
2
3
4

When number becomes 5, the loop stops.

12. continue Statement

The continue statement skips the remaining code in the current iteration and moves to the next iteration.

Example

fn main() {
for number in 1..=5 {
if number == 3 {
continue;
}

println!("{}", number);
}
}

Output:

1
2
4
5

Number 3 is skipped.

break vs continue


break

continue

Terminates the loop

Skips the current iteration

Does not execute further iterations

Continues with the next iteration

Example: break;

Example: continue;

13. Returning a Value from loop

Rust allows a loop to return a value using break.

Example

fn main() {
let mut counter = 0;

let result = loop {
counter += 1;

if counter == 5 {
break counter * 2;
}
};

println!("Result: {}", result);
}

Output:

Result: 10

Explanation:

  1. The loop continues until counter becomes 5.
  2. break counter * 2 stops the loop and returns 10.
  3. The returned value is stored in result.

14. Loop Labels

When using nested loops, loop labels help us specify which loop should be stopped or continued.

Rust loop labels start with a single quotation mark.

Example

fn main() {
let mut count = 0;

'outer: loop {
let mut inner = 0;

loop {
if inner == 3 {
break 'outer;
}

inner += 1;
count += 1;
}
}

println!("Count: {}", count);
}

Output:

Count: 3

Here, break 'outer terminates the outer loop directly.

15. Comparison of Rust Loops


Loop

Purpose

loop

Repeats indefinitely until stopped

while

Repeats while a condition is true

for

Iterates over a range or collection

16. Important Summary


Concept

Purpose

if

Executes code when a condition is true

else

Executes code when the if condition is false

else if

Checks multiple conditions

Nested if

Places one if statement inside another

match

Matches a value against patterns

loop

Creates an indefinite loop

while

Repeats while a condition is true

for

Iterates over ranges and collections

break

Terminates a loop

continue

Skips the current iteration

Loop labels

Control nested loops