Back to questions
JuniorTheoryCommon
What happens when this program is built and run?
fn main() {
let n = 1;
if n {
println!("yes");
} else {
println!("no");
}
}It does not compile
- Expecting
yes, as in C or Python: Rust has no truthiness, anifcondition must be abool, soif nis error E0308 until it readsif n != 0. - Expecting a panic at run time: the type of the condition is checked while building, so the compiler rejects
if nand no program is produced.
- How would you change the condition so that the program prints
yes? - Why does an
ifwhose branches return ani32and a&strnot compile?