Skip to content

Instantly share code, notes, and snippets.

@AOx0
Created May 4, 2021 05:29
Show Gist options
  • Save AOx0/6b7666579ca6532af34d106d28d31bed to your computer and use it in GitHub Desktop.
Save AOx0/6b7666579ca6532af34d106d28d31bed to your computer and use it in GitHub Desktop.
fn main() {
for n in 1..=100 {
println!("{}", get_fizz_buzz(n))
}
}
trait MultiplePush {
fn push_strs(&mut self, strs: &[&str]);
}
impl MultiplePush for String {
fn push_strs(&mut self, strs: &[&str]) {
for str_ in strs {
self.push_str(str_);
}
}
}
fn get_fizz_buzz(n: i32) -> String {
let mut result = String::with_capacity(64);
result.push_strs(&[
if n % 3 == 0 { "Fizz" } else { "" },
if n % 5 == 0 { "Buzz" } else { "" },
]);
if result.len() == 0 {
result = n.to_string()
}
result
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment