Skip to content

Instantly share code, notes, and snippets.

@wjlroe
Last active May 2, 2016 21: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 wjlroe/dec2b2d198161b8454d0 to your computer and use it in GitHub Desktop.
Save wjlroe/dec2b2d198161b8454d0 to your computer and use it in GitHub Desktop.
FIZZBUZZ!!!
use std::fmt;
trait Fizzbuzzable {
fn next(&self) -> Box<Fizzbuzzable>;
fn string(&self) -> String;
}
struct A1(i64);
struct A2(i64);
struct A3(i64);
struct B1(i64);
struct B2(i64);
struct C1(i64);
struct D1(i64);
struct D2(i64);
struct D3(i64);
struct E1(i64);
struct F1(i64);
struct F2(i64);
struct G1(i64);
struct G2(i64);
struct G3(i64);
impl Fizzbuzzable for A1 {
fn next(&self) -> Box<Fizzbuzzable> {
let A1(i) = *self;
Box::new(A2(i + 1)) as Box<Fizzbuzzable>
}
fn string(&self) -> String {
let A1(i) = *self;
fmt::format(format_args!("{}", i))
}
}
impl Fizzbuzzable for A2 {
fn next(&self) -> Box<Fizzbuzzable> {
let A2(i) = *self;
Box::new(A3(i + 1)) as Box<Fizzbuzzable>
}
fn string(&self) -> String {
let A2(i) = *self;
fmt::format(format_args!("{}", i))
}
}
impl Fizzbuzzable for A3 {
fn next(&self) -> Box<Fizzbuzzable> {
let A3(i) = *self;
Box::new(B1(i + 1)) as Box<Fizzbuzzable>
}
fn string(&self) -> String {
"fizz ".to_string()
}
}
impl Fizzbuzzable for B1 {
fn next(&self) -> Box<Fizzbuzzable> {
let B1(i) = *self;
Box::new(B2(i + 1)) as Box<Fizzbuzzable>
}
fn string(&self) -> String {
let B1(i) = *self;
fmt::format(format_args!("{}", i))
}
}
impl Fizzbuzzable for B2 {
fn next(&self) -> Box<Fizzbuzzable> {
let B2(i) = *self;
Box::new(C1(i + 1)) as Box<Fizzbuzzable>
}
fn string(&self) -> String {
"buzz ".to_string()
}
}
impl Fizzbuzzable for C1 {
fn next(&self) -> Box<Fizzbuzzable> {
let C1(i) = *self;
Box::new(D1(i + 1)) as Box<Fizzbuzzable>
}
fn string(&self) -> String {
"fizz ".to_string()
}
}
impl Fizzbuzzable for D1 {
fn next(&self) -> Box<Fizzbuzzable> {
let D1(i) = *self;
Box::new(D2(i + 1)) as Box<Fizzbuzzable>
}
fn string(&self) -> String {
let D1(i) = *self;
fmt::format(format_args!("{}", i))
}
}
impl Fizzbuzzable for D2 {
fn next(&self) -> Box<Fizzbuzzable> {
let D2(i) = *self;
Box::new(D3(i + 1)) as Box<Fizzbuzzable>
}
fn string(&self) -> String {
let D2(i) = *self;
fmt::format(format_args!("{}", i))
}
}
impl Fizzbuzzable for D3 {
fn next(&self) -> Box<Fizzbuzzable> {
let D3(i) = *self;
Box::new(E1(i + 1)) as Box<Fizzbuzzable>
}
fn string(&self) -> String {
"fizz ".to_string()
}
}
impl Fizzbuzzable for E1 {
fn next(&self) -> Box<Fizzbuzzable> {
let E1(i) = *self;
Box::new(F1(i + 1)) as Box<Fizzbuzzable>
}
fn string(&self) -> String {
"buzz ".to_string()
}
}
impl Fizzbuzzable for F1 {
fn next(&self) -> Box<Fizzbuzzable> {
let F1(i) = *self;
Box::new(F2(i + 1)) as Box<Fizzbuzzable>
}
fn string(&self) -> String {
let F1(i) = *self;
fmt::format(format_args!("{}", i))
}
}
impl Fizzbuzzable for F2 {
fn next(&self) -> Box<Fizzbuzzable> {
let F2(i) = *self;
Box::new(G1(i + 1)) as Box<Fizzbuzzable>
}
fn string(&self) -> String {
"fizz ".to_string()
}
}
impl Fizzbuzzable for G1 {
fn next(&self) -> Box<Fizzbuzzable> {
let G1(i) = *self;
Box::new(G2(i + 1)) as Box<Fizzbuzzable>
}
fn string(&self) -> String {
let G1(i) = *self;
fmt::format(format_args!("{}", i))
}
}
impl Fizzbuzzable for G2 {
fn next(&self) -> Box<Fizzbuzzable> {
let G2(i) = *self;
Box::new(G3(i + 1)) as Box<Fizzbuzzable>
}
fn string(&self) -> String {
let G2(i) = *self;
fmt::format(format_args!("{}", i))
}
}
impl Fizzbuzzable for G3 {
fn next(&self) -> Box<Fizzbuzzable> {
let G3(i) = *self;
Box::new(A1(i + 1)) as Box<Fizzbuzzable>
}
fn string(&self) -> String {
"fizzbuzz ".to_string()
}
}
fn main() {
let limit = 100;
let mut v = Box::new(A1(1)) as Box<Fizzbuzzable>;
for _ in 0..limit {
println!("{}", v.string());
v = v.next();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment