Rullst AcademyRullst Academy/Lesson
Rust: from idea to program · Lesson 07 / 08

What if there is no bonus?

Not every round earns a bonus. Instead of pretending a value always exists, we will represent both possibilities in the type itself.

Learning paths10 min

Let's discover together

  1. 1

    Option<u32> means “there may be a u32.” Some(5) contains 5; None represents absence. None is not a hidden number.

  2. 2

    Our bonus function takes correct answers. At three or more it returns Some(5); below three it returns None. It declares this with -> Option<u32>.

  3. 3

    match examines the value: Some(extra) => base + extra, None => base. extra receives the number inside Some; in the None case, no extra is added.

  4. 4

    Handling both variants makes absence visible. Some(0) still contains a value, zero; None says there is no value. Avoid assuming presence when the type permits absence.

✦
YOUR MISSION

Interpret Some and None and handle both paths with match.

Now it's your turn

Discovery workshop

Open both possibilities

Rule: bonus(correct) returns Some(5) if correct >= 3; otherwise None.
Build your answers and check when you are ready.
A moment to understand

Which case says there is no bonus value?

I need a hint

Some always holds a value, even when that value is zero.

Loading your journey…

Take it with you

Think of a search that may not find an item. How would Option remind you that “not found” is a normal possibility?

Show me the Rust behind it

This part is optional. You don't need to memorize the code to continue.

fn bonus(correct: u32) -> Option<u32> {
    if correct >= 3 { Some(5) } else { None }
}

fn main() {
    let total = match bonus(3) {
        Some(extra) => 30 + extra,
        None => 30,
    };
    println!("{total}");
}
At your own pace

My files and GitHub

Every lesson can become your own record: code, notes or a practice plan. Saving a file to GitHub is optional and does not change your XP.

Student guide

The editor and downloads open when this lesson is available to your account. Check access below or return to the track map.

Checking your access to this lesson…

Rullstpetbot

AI can make mistakes. Check the lesson material.