What does “mismatched types: expected `()`” mean when using an if expression?

后端 未结 1 1044
一向
一向 2020-11-30 15:09

I tried to implement fizzbuzz in Rust and failed with some arcane error:

fn main() {
    let mut i = 1;

    while i < 100 {
        println!(
                    


        
相关标签:
1条回答
  • 2020-11-30 16:07

    The problem is that if i % 3 == 0 { "Fizz" } returns either unit () or &'static str. Change the if expressions to return the same type in both cases, for example by adding a else { "" }.

    0 讨论(0)
提交回复
热议问题