Rust Foundations·Practice drills 5

Drill 11: Explain each line — Functions and control flow

Explain what each line of the example does.

Intermediate 5 min read Lesson 112 of 300 Challenge included
What you will be able to do
  • Explain what each line of the example does.

Practice task

Explain what each line of the example does.

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

Explain what each line of the example does.

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 112 of 300 · 0% of course