Day 5 50 min intermediate

Control Flow

Learn to make decisions with if expressions and repeat tasks with various loop types

Learning Objectives

  • Use 'if' expressions (and learn why they are expressions)
  • Understand 'loop', 'while', and 'for' loops
  • Iterate through collections with 'for' and 'range'

if Expressions

In Rust, if is an expression, not just a statement. This means it returns a value.

main.rs
1
2
3
4
5
6
7
8
9
fn main() {
    let number = 3;

    if number < 5 {
        println!("condition was true");
    } else {
        println!("condition was false");
    }
}

Using if in a let Statement

Since if is an expression, we can use it on the right side of a let statement:

1
2
let condition = true;
let number = if condition { 5 } else { 6 };
Type Matching
The values in the if and else arms must have the same type. You cannot return an integer from one and a string from the other.

Repetition with Loops

Rust has three kinds of loops: loop, while, and for.

1. The loop

loop tells Rust to execute a block of code over and over again forever or until you explicitly tell it to stop.

main.rs
1
2
3
4
5
6
7
8
9
fn main() {
    let mut count = 0;
    loop {
        count += 1;
        if count == 3 {
            break; // Stop the loop
        }
    }
}

Returning values from loops: You can add a value after break to return it from the loop:

1
2
3
4
5
6
let result = loop {
    counter += 1;
    if counter == 10 {
        break counter * 2;
    }
};

2. The while Loop

Runs as long as a condition is true.

1
2
3
4
5
let mut number = 3;
while number != 0 {
    println!("{}!", number);
    number -= 1;
}

3. The for Loop

The most common loop in Rust. It’s used to iterate over a collection or a range.

main.rs
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
fn main() {
    let a = [10, 20, 30, 40, 50];

    for element in a {
        println!("the value is: {}", element);
    }

    // Using a range (1 to 3, excluding 4)
    for number in 1..4 {
        println!("{}!", number);
    }
}
Ranges
1..4 is a range from 1 to 3. 1..=4 is a range from 1 to 4.

Summary

Today you learned:

  • if is an expression and can be used to assign values.
  • loop is for infinite repetition (can return values via break).
  • while is for conditional repetition.
  • for is the safest and most common way to iterate through collections and ranges.

Practice Exercise

  1. Write a program that prints numbers from 1 to 20, but for multiples of 3 print “Fizz” and for multiples of 5 print “Buzz” (FizzBuzz!).
  2. Use a for loop to reverse an array.
  3. Use loop to find the first power of 2 greater than 1000.

Next Steps

Tomorrow, we’ll cover the most important concept in Rust: Ownership! This is what makes Rust different from every other language.

Quiz

Complete this quiz with a minimum score of 80% to mark Day 5 as complete.

Loading quiz...

Discussion

Have questions or want to discuss this lesson? Join the conversation below!