0

When I compile this code:

let n: u32 = 1;
println!("{} dice on the table", if n == 0 { "no more" } else { n });

The compiler gives me the error:

error[E0308]: if and else have incompatible types
 --> src/main.rs:3:69
  |
3 |     println!("{} dice on the table", if n == 0 { "no more" } else { n });
  |                                                  ---------          ^ expected `&str`, found `u32`
  |                                                  |
  |                                                  expected because of this

What is the best way to solve the problem?

Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
VoidStar
  • 936
  • 1
  • 7
  • 17
  • [The duplicate applied to your situation](https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=67ff3b8de95da6ea2836c51b9ce97722), or if you wanted it [all in one expression](https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=6411225aed36de252c662254a94da1a7). – Shepmaster Mar 04 '20 at 20:23
  • Thanks, your code snippets helped a lot. I found a Discord server where I can ask my noob questions. – VoidStar Mar 04 '20 at 20:51

0 Answers0