re: 5 Powerful Programming Languages to Stretch Your Brain VIEW POST

FULL DISCUSSION
 

Nice article, I would have chosen the following construct for the Rust example since pattern matching is more idiomatic in Rust than a bunch of if/else.

Something like this :

fn fizzbuzz(i: u8) {
  match i {
    x if x % 15 == 0 => {
      println!("FizzBuzz");
    },
    x if x % 3 == 0 => {
      println!("Fizz");
    },
    x if x % 5 == 0 => {
      println!("Buzz");
    },
    _ => {
      println!("{}", i);
    }
  }
}

Its not quite as compact as your example but you can remove a level of indentation by just having one liners like x if x % 15 == 0 => println!("FizzBuzz"), if that's your thing.

One advantage to pattern matching in Rust is that it forces you to exhaust all possibilities when comparing values. This avoids forgetting the else or a particular case for a number.

Excellent article !

 

Thanks for sharing! I didn't actually write the FizzBuzz solutions, they are from a GitHub repo. I appreciate your input though, super useful 🤠

 

Way ahead of you, I already did! It's in a comment in each snippet.

code of conduct - report abuse