Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

> and insisting a default clause

It does not insist on a default clause it requires match completeness, which depending on the matched value may require a default clause (e.g. it does for integrals[0], it does not for enums as you can match each case individually)

> it's just like C's switch, right?

Only in its most simplistic form (though even then it does not ever fall through — whether by default or optionally — is type-safe and requires match-completeness), match performs refutable pattern matching on possibly complex values and allows additional per-match conditionals.

    match foo {
        // complex destructuring and multiple patterns for a case
        Some((42, _)) | Some((55, _)) => { println!("1") }
        // simple destructuring + conditional
        Some(a) if a.0 % 5 == 0 => { println!("2") }
        // matching + wildcard
        Some(..) => { println!("3") }
        // trivial matching
        None => { println!("4") }
    }
or

    match (i % 3, i % 5) {
        (0, 0) => {}
        (0, _) => {}
        (_, 0) => {}
        _ => {}
    }
[0] because the completeness checking doesn't really handle non-enum values currently


Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: