Rust Foundations·Practice drills 8

Drill 20: Make it smaller — Functions and control flow

Write a smaller version of the example that keeps the core idea.

Intermediate 5 min read Lesson 202 of 300 Challenge included
What you will be able to do
  • Write a smaller version of the example that keeps the core idea.

Practice task

Write a smaller version of the example that keeps the core idea.

main.rsRust
fn level(minutes: u32) -> u32 {
    if minutes < 10 {
        1
    } else if minutes < 20 {
        2
    } else {
        3
    }
}

fn main() {
    let level = level(12);
    println!("level {}", level);

    let mut count = 0;
    loop {
        count += 1;
        if count >= 3 {
            break;
        }
    }
    println!("looped {} times", count);

    while count < 5 {
        count += 1;
    }
    println!("count now {}", count);

    for i in 1..4 {
        println!("lesson {}", i);
    }

    for i in 1..=3 {
        println!("inclusive {}", i);
    }
}
Your turn

Write your answer

Easy

Write a smaller version of the example that keeps the core idea.

TEXT · starter
Your answer…
Study tools

Make it yours.

Keep a private note for this lesson, then turn the key ideas into flashcards you can revisit.

Sign in to save notes and flashcards.Both are private to your account and stay with your progress.
Sign in
Finished reading?

Lock in what you just learned.

Mark this lesson complete to save your place and move one step closer to the certificate.

Lesson 202 of 300 · 0% of course