rust-lang / rust-roadmap-2017

Tracking Rust's roadmap
215 stars 12 forks source link

Improved match ergonomics around references #24

Open nrc opened 7 years ago

nrc commented 7 years ago

Point of contact

@nrc

Overview

Make match easier to use and less boilerplate-y by auto-dereferencing in the condition expression and inferring refs in the patterns.

E.g.,

fn foo(x: &AnEnum) {
    match x {
        AnEnum::VariantA(y) => { ... }
        AnEnum::VariantB(y, z) => { ... }
    }
}

rather than (currently):

fn foo(x: &AnEnum) {
    match *x {
        AnEnum::VariantA(ref y) => { ... }
        AnEnum::VariantB(ref y, ref z) => { ... }
    }
}

Status

Accepted as RFC 2005.

cc #17

solson commented 7 years ago

If we infer ref in patterns, will we ever need to explicitly tell Rust not to add ref, like we need to use move on closures?

(Let me know if I should wait for the actual RFC to get posted and move this comment there.)

aturon commented 7 years ago

Updated with RFC.

liigo commented 7 years ago

match type &AnEnum against AnEnum? I don't think it's kind of 'pattern matching'.

cramertj commented 7 years ago

Note: this feature was accepted as RFC #2005.