In this exercise, we will implement our first tiny program: FizzBuzz. FizzBuzz is easy to implement, but allows for application of Rust patterns in a very clean fasion.

You will learn:

  • How to write a simple Rust program

  • How to create and return owned String s

  • How to use conditionals

  • How to format strings with and without printing them to the system console

Task

  1. Use Cargo to create a new project called fizzbuzz

  2. Implement the following function:

    fn fizzbuzz(i: u32) -> String {
        ///....
    }

    Implementing the following rules:

    • If i is divisible by 3, return String::from("Fizz")

    • If i is divisible by 5, return String::from("Buzz")

    • If i is divisible by both 3 and 5, return String::from("FizzBuzz")

    • Return the number as a String, otherwise, using i.to_string()

    • Test the function

  3. Write a main function that loops from 1 to 100 using for

    • Call the fizzbuzz function inside the loop and store the result in the variable fizz.

    • Print the returned value using println!("{}", fizz)

Help

This section gives partial solutions to look at or refer to.

In general, we also recommend to use the Rust documentation to figure out things you are missing to familiarise yourself with it. If you ever feel completely stuck or that you haven’t understood something, please hail the trainers quickly.

Getting Started

Create a new binary Cargo project, check the build and see if it runs:

$ cargo new fizzbuzz
$ cd fizzbuzz
$ cargo run

Creating Strings

The recommended ways to get a String type for this exercise are:

let string = String::from("Fizz");
let i = 4;
let string = i.to_string();

Counting from 1 to 100

If you have issues that your program only counts to 99, be aware that the ..-range syntax in Rust is end-exclusive. Either move the bound or use the inclusive range syntax.

for i in 1..5 {
    // Only gives you 1, 2, 3, 4
}

for i in 1..=5 {
    // Gives you 1, 2, 3, 4 and 5
}

Returning data

If you have issues returning data from multiple branches of your solution, liberally use return.

if x % 5 == 0 {
    return String::from("Buzz");
}

Printing to console

The recommended way to print to the console in this exercise is println!. println! always needs a format string - it uses {} as a placeholder to mean print the next argument, like Python 3 or C#.

let s = String::from("Fizz");
println!("The value is s is {}. That's nice.", s);

Testing

Testing functions are annotated using the #[test] attribute, assertions use assert!(expr == expr) or assert_eq!(expr, expr).

#[test]
fn my_test() {
    assert_eq!(1, 1);
}