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) },
}
}
}
3 Answers 3
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)
}
}
}
-
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– Shepmaster2016年05月04日 16:27:26 +00:00Commented 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– user2016年05月17日 12:04:27 +00:00Commented 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– Frank Buss2021年09月11日 11:51:59 +00:00Commented Sep 11, 2021 at 11:51
I don't think the
match
adds a lot here (and I love usingmatch
). I'd just use chained if-else blocks.There's no need to format a string literal; just put that string in the
println!
call directly.Use
{}
for user-facing output;{:?}
is for developer-facing output.There's no need for parenthesis around the
if
condition. Standaloneif
s 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) }
}
}
-
1\$\begingroup\$ Neat. thanks. you right. I like that the outer most curly braces are all in alignment. \$\endgroup\$user– user2016年05月04日 14:50:38 +00:00Commented May 4, 2016 at 14:50
-
\$\begingroup\$ @user, some OCD in you right there :) \$\endgroup\$iamcastelli– iamcastelli2020年06月26日 10:59:03 +00:00Commented Jun 26, 2020 at 10:59
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 { "" }
}
-
2\$\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 theto_string
. 4check
is a poor name. \$\endgroup\$Shepmaster– Shepmaster2017年05月12日 14:40:36 +00:00Commented May 12, 2017 at 14:40
102
, instead of101
? This seem to print to101
, beyond 100 \$\endgroup\$