Core Beginner

02  Control flow

Loops and conditionals: sum a range, then write the classic fizzbuzz for a single number.

Step 2 / 3

Return "fizz" / "buzz" / "fizzbuzz", or the number, in fizzbuzz.

Hints
  • if/else is an expression; format!("{}", n) turns a number into a String.
02_control_flow.rs
Reset to starterCompiled in a sandbox; hidden checks must pass.
Reveal solution
fn fizzbuzz(n: u32) -> String {
    if n % 15 == 0 {
        String::from("fizzbuzz")
    } else if n % 3 == 0 {
        String::from("fizz")
    } else if n % 5 == 0 {
        String::from("buzz")
    } else {
        format!("{}", n)
    }
}