The following is obviously not working:
fn main() {
for i in range(1i, 101) {
println!("{}", if i % 15 == 0 {
"Fizzbuzz"
} else if i % 5 == 0 {
"Buzz"
} else if i % 3 == 0 {
"Fizz"
} else {
i
});
};
}
It can be made work like this:
fn main() {
for i in range(1i, 101) {
println!("{}", if i % 15 == 0 {
"Fizzbuzz".to_string()
} else if i % 5 == 0 {
"Buzz".to_string()
} else if i % 3 == 0 {
"Fizz".to_string()
} else {
i.to_string()
});
}
}
But what is the most elegant (probably idiomatic) way to make it work in a similar fashion using if/else with expressions?