Created
May 4, 2021 05:29
-
-
Save AOx0/6b7666579ca6532af34d106d28d31bed to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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