DEV Community

Discussion on: 5 Powerful Programming Languages to Stretch Your Brain

Collapse
 
tristanred profile image
Tristan Dubé

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 !

Collapse
 
jacobherrington profile image
Jacob Herrington (he/him)

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

Collapse
 
youngsteveo profile image
Stephen Young

You should cite your source in the article.

Thread Thread
 
jacobherrington profile image
Jacob Herrington (he/him)

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