if Expressions
if is an expression and can return a value. Conditions must be bool.
Example
fn main() {
let n = 7;
let kind = if n % 2 == 0 { "even" } else { "odd" };
println!("{}", kind);
if n > 5 && n < 10 { println!("mid"); }
}Result
odd mid
Exercise
Return from if.
let x = c { 1 } else { 2 };
Quiz Test
These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.