write fizz buzz in rust
- User
write fizz buzz in rust
Assistant
Here's a FizzBuzz implementation in Rust:
fn main() { for i in 1..=100 { match (i % 3, i % 5) { (0, 0) => println!("FizzBuzz"), (0, _) => println!("Fizz"), (_, 0) => println!("Buzz"), _ => println!("{}", i), } } }This uses pattern matching on a tuple of the remainders when dividing by 3 and 5. Here's an alternative approach using if-else statements:
fn main() { for i in 1..=100 { if i % 15 == 0 { println!("FizzBuzz"); } else if i % 3 == 0 { println!("Fizz"); } else if i % 5 == 0 { println!("Buzz"); } else { println!("{}", i); } } }Both versions will print numbers 1 through 100, replacing multiples of 3 with "Fizz", multiples of 5 with "Buzz", and multiples of both with "FizzBuzz".
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.