I can’t help but suspect it doesn’t offer much and you may as well just use match statements for whenever you want pattern matching, however many times it might be slightly more verbose than what you could do with if let.

I feel like I’d easily miss that pattern matching was happening with if let but will always know with match what’s happening and have an impression of what’s happening just from the structure of the code.

  • Lmaydev
    link
    fedilink
    English
    arrow-up
    8
    ·
    edit-2
    7 months ago
    // Make optional of type Option<i32>
    let optional = Some(7);
    
    match optional {
        Some(i) => {
            println!("This is a really long string and `{:?}`", i);
            // ^ Needed 2 indentations just so we could destructure
            // `i` from the option.
        },
        _ => {},
        // ^ Required because `match` is exhaustive. Doesn't it seem
        // like wasted space?
    };
    

    I think the rust book gives a good example. The match syntax is just a waste here.

    So if you have a single expression that you want to match I think it is worth it.

    As with most syntax, the more you read it the more you’ll just spot it without thinking.