Enum variants as types?

Is Rust planning to allow handling of enum variants as types?

enum Foo { A, B(u32) }

fn bar1() -> Foo::A {
    Foo::A
}

fn bar2(x: Foo::A) {
}

impl Foo {
    fn bar3(&mut self: Foo::B(ref mut)) {
        match self {
            &mut Foo::B(ref mut x) => *x += 1;
        }
    }
}

fn main() {}

This code means that (unlike Haskell) you can't call bar2() with a Foo. You must call it with a Foo::A.

This feature can speed up some code (because some unwrap() get removed or become a no-op), make some code more provably correct (because you have more static information about the values, so you don't need to handle some impossible cases), and can avoid some usages of Option/Result (because the code has to handle less impossible cases).

2 Likes

https://github.com/rust-lang/rfcs/pull/1450

4 Likes

I didn't find it in a quick search, thank you. I've copied my comment in that thread.