From 53b30368251fa4c8d9d3101f0e5687da097d560e Mon Sep 17 00:00:00 2001 From: Rust & Python Date: Wed, 29 Sep 2021 21:29:30 +0530 Subject: [PATCH] feat: Add fizzbuzz.rs exercise --- exercises/fizzbuzz.rs | 49 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 exercises/fizzbuzz.rs diff --git a/exercises/fizzbuzz.rs b/exercises/fizzbuzz.rs new file mode 100644 index 00000000..bd8b38c1 --- /dev/null +++ b/exercises/fizzbuzz.rs @@ -0,0 +1,49 @@ +// fizzbuzz challenge + +// Make a simple code which fulfills the given tests +// Starting code is already given to you :) + +/* Write a program that substitues the following: + * mulitples of 3 => "fizz" + * multiples of 5 => "buzz" + * mulitples of 3 and 5 => "fizzbuzz" + */ + +// I AM NOT DONE + +pub fn fizzbuzz(num: i32) -> String { + match (num % 3, num % 5) { + (0, 0) => "fizzbuzz".to_string(), + (0, _) => "fizz".to_string(), + (_, 0) => "buzz".to_string(), + (_, _) => num.to_string(), + } +} + +// Don't change this part(the tests) + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn fizz() { + assert_eq!(fizzbuzz(3), "fizz".to_string()); + assert_eq!(fizzbuzz(6), "fizz".to_string()); + assert_eq!(fizzbuzz(53), "53".to_string()); + } + + #[test] + fn buzz() { + assert_eq!(fizzbuzz(5), "buzz".to_string()); + assert_eq!(fizzbuzz(10), "buzz".to_string()); + assert_eq!(fizzbuzz(13), "13".to_string()); + } + + #[test] + fn fizz_and_buzz() { + assert_eq!(fizzbuzz(15), "fizzbuzz".to_string()); + assert_eq!(fizzbuzz(45), "fizzbuzz".to_string()); + assert_eq!(fizzbuzz(31), "31".to_string()); + } +}