Skip to content

Instantly share code, notes, and snippets.

@sambaiz
Last active September 5, 2017 10:53
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 sambaiz/fd8839952908247617098083f0c1075c to your computer and use it in GitHub Desktop.
Save sambaiz/fd8839952908247617098083f0c1075c to your computer and use it in GitHub Desktop.
RustでFizzBuzz
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