Skip to content

Instantly share code, notes, and snippets.

@carols10cents
Forked from anonymous/playground.rs
Last active October 30, 2015 18:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save carols10cents/b48cbd7cc45c225dfba8 to your computer and use it in GitHub Desktop.
Save carols10cents/b48cbd7cc45c225dfba8 to your computer and use it in GitHub Desktop.
fizzbuzz to illustrate conditionals
// Make these tests pass by filling in conditionals where the blanks currently are!
// Scroll down for hints :)
pub fn fizz_buzz(i: i32) -> String {
__________ {
"FizzBuzz".to_string()
} ___________ {
"Fizz".to_string()
} ___________ {
"Buzz".to_string()
} __________ {
i.to_string()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn three_is_fizz() {
assert_eq!("Fizz", fizz_buzz(3));
}
#[test]
fn five_is_buzz() {
assert_eq!("Buzz", fizz_buzz(5));
}
#[test]
fn fifteen_is_fizzbuzz() {
assert_eq!("FizzBuzz", fizz_buzz(15));
}
#[test]
fn two_is_two() {
assert_eq!("2", fizz_buzz(2));
}
}
// This program takes an integer and returns a string-- if the integer is divisible by
// 3, return "Fizz", divisible by 5 means to return "Buzz", and if the integer is divisible
// by both 3 and 5, return "FizzBuzz". For all other nubers, return them converted to a string.
// Conditionals in Rust are done using `if`, `else if`, and `else`. A function you might want to
// use is `%`, called the "mod operator"-- it returns the remainder from a division operator.
// When a number is divisible evenly by another number, the mod operation returns 0.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment