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

match with conditions

  • The variable in each arm can be all the same or they can be different.
fn number(num: i32) {
    match num {
        x if x > 30 => println!("{} is above 30", x),
        x if x > 20 => println!("{} is above 20", x),
        neg if neg < 0 => println!("{} is negative", neg),
        _ => println!("other: {num}"),
    }
}

fn main() {
    number(40);
    number(30);
    number(0);
    number(-7);
}
40 is above 30
30 is above 20
other: 0
-7 is negative