Created
July 20, 2016 00:54
-
-
Save LeMeteore/17ad146b1c69b96e24e51f56f6efbe41 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
#![allow(dead_code)] | |
use std::fmt::Display; | |
use std::fmt::Formatter; | |
use std::fmt::Result; | |
enum Fzb { | |
Value(u64), | |
Fizz(u64), | |
Buzz(u64), | |
FizzBuzz(u64), | |
} | |
// Display is a trait | |
// implemented for letting rust being able | |
// to display our Fzb enum | |
impl Display for Fzb { | |
fn fmt(&self, f: &mut Formatter) -> Result { | |
match self { | |
// self was passed by reference, self is Fzb | |
&Fzb::FizzBuzz(_) => write!(f, "FizzyBuzzy"), | |
&Fzb::Fizz(_) => write!(f, "Fizzy"), | |
&Fzb::Buzz(_) => write!(f, "Buzzy"), | |
&Fzb::Value(v) => write!(f, "{}", v), | |
} | |
} | |
} | |
fn fzbz(n: u64) { | |
match n { | |
n if n % 15 == 0 => println!("fizzbuzz"), | |
n if n % 5 == 0 => println!("fizz"), | |
n if n % 3 == 0 => println!("buzz"), | |
_ => println!("{:?}", n), | |
}; | |
// after adding the semicolon, the whole match statement is returned | |
// and the type of the whole statement is Unit | |
// Unit: | |
} | |
fn fzbz2(n: u64) -> Fzb { | |
match n { | |
n if n % 15 == 0 => Fzb::FizzBuzz(n), | |
n if n % 5 == 0 => Fzb::Fizz(n), | |
n if n % 3 == 0 => Fzb::Buzz(n), | |
n => Fzb::Value(n), | |
} | |
// after removing the semicolon, not the whole match statement is returned | |
// What is returned is of the branches, of type Fzb | |
} | |
// main1 | |
fn main() { | |
for n in (1..100).map(fzbz2) { | |
println!("{}", n); | |
} | |
// let i = 9i32; | |
// match i { | |
// m @ 10...100 => println!("{} between 10 & 100 included", m), | |
// l => println!("{} not between 10 & 100 included", l), | |
// } | |
} | |
// fizzbuzz from the youtube tutorial https://www.youtube.com/watch?v=sv9fTlU7SCA |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment