Multiple function definitions with non-overlapping argument patterns

I have a code similar to the following:

enum MyEnum { A, B }
fn my_func(x: MyEnum, other: X, args: Y) {
    match x {
        A => processA(other, args),
        B => processB(other, args),

It would be great if instead I could define my_func twice, with different x arguments, just like in Haskell:

fn my_func(A: MyEnum, other: X, args: Y) {
    // move contents of processA here
fn my_func(B: MyEnum, other: X, args: Y) {
    // move contents of processB here

Is anything like this possible?

That would require types for enum variants, which Rust won't have anytime soon:

Apart from that it would require type-based function overloading, which isn't planned at all, because it interacts poorly with type inference.

Thanks for a quick response! Too bad this isn't possible now.

But I think it could be easily implemented as a syntax sugar, without any changes to the type inference. Multiple definitions could be desugared into a single definition with a match as the first and only statement. Checking that all the possibilities are covered and all are disjoint could follow the same rules.

Should I suggest this on the internals forum, or am I missing something?

Rust in general doesn't do "nice to have" syntax sugar. The syntax sugar added so far either enables completely new uses (like impl Trait), or is very widely applicable (like if let), or fixes common painful usability problems (like match ergonomics).

If this is a limited to only enums, and all it does is generate a match, then you probably can do it yourself with a macro.


I see. Indeed, this seems like a rare need. Thanks for saving me the time of finding out on the internals forum.