Last active
September 5, 2017 10:53
-
-
Save sambaiz/fd8839952908247617098083f0c1075c to your computer and use it in GitHub Desktop.
RustでFizzBuzz
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
use std::fmt; | |
fn main() { | |
println!("Hello, world!"); | |
for i in 1..100 { | |
println!("{}", fizz_buzz(i)) | |
} | |
} | |
enum FBN<'a> { | |
FString(&'a str), | |
FInteger(i32), | |
} | |
impl<'a> From<&'a str> for FBN<'a> { | |
fn from(str: &'a str) -> Self { | |
FBN::FString(str) | |
} | |
} | |
impl<'a> From<i32> for FBN<'a> { | |
fn from(num: i32) -> Self { | |
FBN::FInteger(num) | |
} | |
} | |
impl<'a> fmt::Display for FBN<'a> { | |
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | |
match self { | |
&FBN::FString(v) => write!(f, "{}", v), | |
&FBN::FInteger(n) => write!(f, "{}", n), | |
} | |
} | |
} | |
fn fizz_buzz<'a>(num: i32) -> FBN<'a> { | |
match num { | |
_ if num % 15 == 0 => FBN::from("FizzBuzz"), | |
_ if num % 3 == 0 => FBN::from("Fizz"), | |
_ if num % 5 == 0 => FBN::from("Buzz"), | |
num => FBN::from(num), | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment