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
Strings -
How to use conditionals
-
How to format strings with and without printing them to the system console
Task
-
Use Cargo to create a new project called
fizzbuzz -
Implement the following function:
fn fizzbuzz(i: u32) -> String { ///.... }Implementing the following rules:
-
If
iis divisible by3, returnString::from("Fizz") -
If
iis divisible by5, returnString::from("Buzz") -
If
iis divisible by both3and5, returnString::from("FizzBuzz") -
Return the number as a String, otherwise, using
i.to_string() -
Test the function
-
-
Write a main function that loops from
1to100usingfor-
Call the
fizzbuzzfunction inside the loop and store the result in the variablefizz. -
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);
}