Skip to content

Instantly share code, notes, and snippets.

@eightbitraptor
Created June 20, 2014 14:07
Show Gist options
  • Save eightbitraptor/ae8115538b35300f1e90 to your computer and use it in GitHub Desktop.
Save eightbitraptor/ae8115538b35300f1e90 to your computer and use it in GitHub Desktop.
fn main() {
for number in range(1,101) {
let output = match number {
x if x % 5 == 0 && x % 3 == 0 => { "FizzBuzz".to_str() }
x if x % 5 == 0 => { "Buzz".to_str() }
x if x % 3 == 0 => { "Fizz".to_str() }
x @ _ => { x.to_str() }
};
println!("{}", output);
}
}
@tbu-
Copy link

tbu- commented Jun 20, 2014

Without the allocation in the "Fizz", the "Buzz" and the "FizzBuzz" case:

fn main() {
    for number in range(1,101) {
        let mut string;
        let output = match number {
            x if x % 5 == 0 && x % 3 == 0 => { "FizzBuzz" }
            x if x % 5 == 0 => { "Buzz" }
            x if x % 3 == 0 => { "Fizz" }
            x @ _ => { string = x.to_str(); string.as_slice() }
        };

        println!("{}", output);
    }
}

@Thiez
Copy link

Thiez commented Jun 20, 2014

fn main() {
  use std::str::{Slice,Owned};
  for number in range(1,16) {
    let output = match number {
      x if x % 5 == 0 && x % 3 == 0 => { Slice("FizzBuzz") }
      x if x % 5 == 0 => { Slice("Buzz") }
      x if x % 3 == 0 => { Slice("Fizz") }
      x @ _ => { Owned(x.to_str()) }
    };
    println!("{}", output);
  }
}

@tbu-
Copy link

tbu- commented Jun 20, 2014

@Thiez Fair enough. :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment