Fizzbuzz
The following example implements the fizzbuzz game.
The rules are:
- A number divisible by
3
returnsFizz
- A number divisible by
5
returnsBuzz
- A number which is divisible by
3
&5
returnsFizzbuzz
- Any other number entered is returned back to the user
State
Let's define an enum
which contains the state of the game.
enum State {
Fizz: (),
Buzz: (),
FizzBuzz: (),
Other: u64,
}
Implementation
We can write a function
which takes an input
and checks its divisibility. Depending on the result a different State
will be returned.
fn fizzbuzz(input: u64) -> State {
if input % 15 == 0 {
State::FizzBuzz
} else if input % 3 == 0 {
State::Fizz
} else if input % 5 == 0 {
State::Buzz
} else {
State::Other(input)
}
}