Skip to content

Instantly share code, notes, and snippets.

@ircnelson
Created July 12, 2020 18:19
Show Gist options
  • Save ircnelson/8cfd3aeba436b7a79f9ff2c3b8d250e0 to your computer and use it in GitHub Desktop.
Save ircnelson/8cfd3aeba436b7a79f9ff2c3b8d250e0 to your computer and use it in GitHub Desktop.
macro_rules! raindrop {
($func:ident, $factor:expr, $text:expr) => {
fn $func<'a>(n: u32) -> Option<&'a str> {
if n % $factor == 0 {
return Some($text);
}
return None;
}
};
}
raindrop!(is_factor_of_three, 3, "Pling");
raindrop!(is_factor_of_five, 5, "Plang");
raindrop!(is_factor_of_seven, 7, "Plong");
pub fn raindrops(n: u32) -> String {
let mut result = String::new();
if let Some(v) = is_factor_of_three(n) {
result.push_str(v);
}
if let Some(v) = is_factor_of_five(n) {
result.push_str(v);
}
if let Some(v) = is_factor_of_seven(n) {
result.push_str(v);
}
if result.is_empty() {
result.push_str(&n.to_string())
}
return result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment