Unit 3.3 - The Tooling of Cargo
Exercise 3.3.1: FizzBuzz
In this exercise, you will practise writing a unit test, and use Rusts benchmarking functionality to help you optimize a FizzBuzz app. You will need cargo-criterion
, a tool that runs benchmarks and creates nice reports. You can install it by running
cargo install cargo-criterion --version=1.1.0
3.3.1.A Testing Fizz Buzz ⭐
Open exercises/3-crate-engineering/3-cargo-tooling/1-fizzbuzz/src/lib.rs
. Create a unit test that verifies the correctness of the fizz_buzz
function. You can use the include_str
macro to include exercises/3-crate-engineering/3-cargo-tooling/1-fizzbuzz/fizzbuzz.out
as a &str
into your binary. Each line of fizzbuzz.out
contains the expected output of the fizz_buzz
function given the line number as input. You can run the test with
cargo test
By default, Rusts test harness captures all output and discards it, If you like to debug your test code using print statements, you can run
cargo test -- --nocapture
to prevent the harness from capturing output.
3.3.1.B Benchmarking Fizz Buzz ⭐⭐
You'll probably have noticed the fizz_buzz
implementation is not very optimized. We will use criterion
to help us benchmark fizz_buzz
. To run a benchmark, run the following command when in the exercises/3-crate-engineering/3-cargo-tooling/1-fizzbuzz/
directory:
cargo criterion
This command will run the benchmarks, and report some statistics to your terminal. It also generates HTML reports including graphs that you can find under target/criterion/reports
. For instance, target/criterion/reports/index.html
is a summary of all benchmark. Open it with your browser and have a look.
Your job is to do some optimization of the fizz_buzz
function, and use cargo-criterion
to measure the impact of your changes. Don't be afraid to change the signature of fizz_buzz
, if, for instance, you want to minimize the number of allocations done by this function. However, make sure that the function is able to correctly produce the output. How fast can you FizzBuzz?