Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

if-else returns a value

  • This expersssion must have an else part!
  • The last statement in both the if and the else part has no ; at the end and thus they are called expressions and not statements.
fn main() {
    let x = 1;
    let y = 2;

    let z = if x < y { x + y } else { x * y };

    println!("{z}");
}