let range = value else return - clever use or abuse of syntax?

57 points andyshiue 62 comments

Normally, when you want to do a early return if a value is out of bounds, you write like this:

fn check_month(month: i32) {
    if month < 1 || month > 12 {
        return;
    }
    // do other things ...
}

However, yesterday I realized you can also write:

fn check_month(month: i32) {
    let 1..=12 = month else {
        return;
    };
    // do other things ...
}

I asked my OpenClaw and got some actual code that uses this pattern. What do you think about it? Should it be encouraged or avoided?