qwen-max-2025-01-25· Alibaba (Qwen) LMArena Human Preference 140K
write fizzbust in rust
- User
write fizzbust in rust
- Assistant
Certainly! Below is a Rust implementation of the classic "FizzBuzz" problem. The program prints numbers from 1 to 100, replacing multiples of 3 with "Fizz", multiples of 5 with "Buzz", and multiples of both 3 and 5 with "FizzBuzz".
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); } } }Explanation:
-
Looping through numbers:
- The
for i in 1..=100loop iterates over numbers from 1 to 100 (inclusive). The..=range syntax ensures that the upper bound (100) is included.
- The
-
Condition checks:
- First, we check if the number is divisible by both 3 and 5 using
i % 15 == 0. This is equivalent to checking divisibility by both 3 and 5 simultaneously. - If the number is not divisible by 15, we check for divisibility by 3 (
i % 3 == 0) and print "Fizz". - Similarly, if the number is not divisible by 3, we check for divisibility by 5 (
i % 5 == 0) and print "Buzz". - If none of these conditions are met, the number itself is printed.
- First, we check if the number is divisible by both 3 and 5 using
-
Output:
- For each iteration, the appropriate output ("Fizz", "Buzz", "FizzBuzz", or the number) is printed to the console.
Example Output:
For the first 15 iterations, the output will look like this:
1 2 Fizz 4 Buzz Fizz 7 8 Fizz Buzz 11 Fizz 13 14 FizzBuzzThis implementation is concise, efficient, and adheres to idiomatic Rust practices.
-
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.