I just learned a bit of new syntax

124 points nee_- 78 comments

While browsing through some code at work, I saw something that made me pause. It looked SO wrong, and yet here we are. It was something to the effect of:

let SomeEnum::Variant(y) = x else {
    return some_error;  
}

which means you can do this:

fn optional_print(x: Option<u32>) {
    let Some(y) = x else {
        return;
    };
    
    println!("{y}");
}

fn main() {
    optional_print(Some(5));
    optional_print(None);
}

Thought some folks here might think it interesting so there you go, you can apparently pattern match enum variants to create a variable and have an else clause after. I thought the only options for this were a let followed by an if let or match statement.