Tsallaka zuwa abun ciki
Justice Tech School

Rust If and Else

Conditions do not need parentheses but must be bool. if is an expression and both branches must return the same type.

Example
let n = 7;
if n % 2 == 0 { println!("even"); } else { println!("odd"); }
let label = if n > 5 { "big" } else { "small" };
println!("{}", label);

Result

odd
big

Exercise

Second branch.

if a {}  if b {}

Quiz Test

Must a condition be a bool?

These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.