Skip to content

Instantly share code, notes, and snippets.

@fernandoescolar
Last active May 26, 2021
Embed
What would you like to do?
FizzBuzz kata in Rust (see revisions)
fn main() {
println!("Fizz Buzz game");
println!("-----------------");
for i in 1..101 {
println!("{}", fizzbuzzer(i));
}
}
fn fizzbuzzer(number: u8) -> String {
match (number % 3, number % 5) {
(0, 0) => String::from("fizzbuzz"),
(0, _) => String::from("fizz"),
(_, 0) => String::from("buzz"),
(_, _) => number.to_string()
}
}
#[cfg(test)]
mod fizzbuzz_tests {
use super::fizzbuzzer;
#[test]
fn when_input_is_1_returns_1() {
assert_fizzbuzzer(1, "1");
}
#[test]
fn when_input_is_2_returns_2() {
assert_fizzbuzzer(2, "2");
}
#[test]
fn when_input_is_3_returns_fizz() {
assert_fizzbuzzer(3, "fizz");
}
// #[test]
// fn when_input_is_4_returns_4() {
// assert_fizzbuzzer(4, "4");
// }
#[test]
fn when_input_is_5_returns_buzz() {
assert_fizzbuzzer(5, "buzz");
}
#[test]
fn when_input_is_6_returns_fizz() {
assert_fizzbuzzer(6, "fizz");
}
#[test]
fn when_input_is_10_returns_buzz() {
assert_fizzbuzzer(10, "buzz");
}
#[test]
fn when_input_is_15_returns_buzz() {
assert_fizzbuzzer(15, "fizzbuzz");
}
fn assert_fizzbuzzer(input: u8, expected: &str) {
let actual = fizzbuzzer(input);
assert_eq!(expected, actual);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment