9
\$\begingroup\$

Can you write an a simpler Rust fizzbuzz program than I have? Use my output or the spec:

Write a program that prints the numbers from 1 to 100. But for multiples of three print “Fizz” instead of the number and for the multiples of five print “Buzz”. For numbers which are multiples of both three and five print “FizzBuzz”.

to write your program. I want to see if it's possible to write an even simpler program.

fn main() {
    for i in 1..102 {
        match i {
            i if (i % 15 == 0) => { println!("{:?}", "FizzBuzz") },
            i if (i % 3 == 0) => { println!("{:?}", "Fizz") },
            i if (i % 5 == 0) => { println!("{:?}", "Buzz") },
            _ => { println!("{:?}", i) },
        }
    }
}
\$\endgroup\$
3
  • 3
    \$\begingroup\$ Related: Why your first FizzBuzz implementation may not work. \$\endgroup\$
    – Shepmaster
    May 4, 2016 at 14:04
  • \$\begingroup\$ @Shepmaster interesting, thanks! It might answer my question. \$\endgroup\$
    – user
    May 4, 2016 at 14:23
  • \$\begingroup\$ @user, I just wish to know why you loop to 102, instead of 101? This seem to print to 101, beyond 100 \$\endgroup\$ Jun 26, 2020 at 11:22

3 Answers 3

27
\$\begingroup\$

I think that match is better, you just do not know how to cook it ;)

fn main() {
    for i in 1..102 {
        match (i%3, i%5) {
            (0, 0) => println!("FizzBuzz"),
            (0, _) => println!("Fizz"),
            (_, 0) => println!("Buzz"),
            (_, _) => println!("{}", i)
        }
    }
}
\$\endgroup\$
3
  • 5
    \$\begingroup\$ +1 This is great because it avoids having the 15, which is really an artifact of the problem. Minor nits: Rust places spaces between binary operators and has trailing commas on the last match arm. I might also have put the == 0 in the match statement and use booleans in the arms. \$\endgroup\$
    – Shepmaster
    May 4, 2016 at 16:27
  • \$\begingroup\$ @passer come to think of it, it's an excellent example of how to use match and patterns. \$\endgroup\$
    – user
    May 17, 2016 at 12:04
  • \$\begingroup\$ Looks nice. But the upper limit is wrong, it prints 101 as well, but it should only print the number and text up and inclusive to 100. I would write it like this: for i in 1..=100 { \$\endgroup\$
    – Frank Buss
    Sep 11, 2021 at 11:51
3
\$\begingroup\$
  1. I don't think the match adds a lot here (and I love using match). I'd just use chained if-else blocks.

  2. There's no need to format a string literal; just put that string in the println! call directly.

  3. Use {} for user-facing output; {:?} is for developer-facing output.

  4. There's no need for parenthesis around the if condition. Standalone ifs actually have a lint to remove those parenthesis.


fn main() {
    for i in 1..102 {
        if i % 15 == 0 { println!("FizzBuzz") }
        else if i % 3 == 0 { println!("Fizz") }
        else if i % 5 == 0 { println!("Buzz") }
        else { println!("{}", i) }
    }
}
\$\endgroup\$
2
  • 1
    \$\begingroup\$ Neat. thanks. you right. I like that the outer most curly braces are all in alignment. \$\endgroup\$
    – user
    May 4, 2016 at 14:50
  • \$\begingroup\$ @user, some OCD in you right there :) \$\endgroup\$ Jun 26, 2020 at 10:59
1
\$\begingroup\$

Depending on what you mean by 'simple'...

fn main() {
  for i in 1 .. 101 {
    let s = check(i,3,"Fizz").to_string() + check(i,5,"Buzz");
    println!("{}",if s == "" { i.to_string() } else { s });
  }
}

fn check(n: i8, d: i8, s: &str) -> &str
{
  if n % d == 0 { s } else { "" }
}
\$\endgroup\$
1
  • 1
    \$\begingroup\$ 1 This formatting (indentation, brace placement, spacing around symbols) is not idiomatic Rust. Please check out rustfmt. 2 I dislike the embedded if — less lines does not a simpler program make. 3 This has many unneeded memory allocations from the to_string. 4 check is a poor name. \$\endgroup\$
    – Shepmaster
    May 12, 2017 at 14:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.