Skip to content

Instantly share code, notes, and snippets.

@LeMeteore
Created July 20, 2016 00:54
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 LeMeteore/17ad146b1c69b96e24e51f56f6efbe41 to your computer and use it in GitHub Desktop.
Save LeMeteore/17ad146b1c69b96e24e51f56f6efbe41 to your computer and use it in GitHub Desktop.
#![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