rfcs icon indicating copy to clipboard operation
rfcs copied to clipboard

Feature Request: Allow enums to have const tagged values

Open kimono-koans opened this issue 3 years ago • 0 comments

This may seem like a small deal, and it may only amount to "This seems like something Rust should have."

Although Rust allows enums to have isized tagged values, like so...

enum MyEnum {
    A = 123,
    B = 456,
}

It seems/feels like Rust should also allow const values, such that when I request D1 or D2, I get back A or B respectively:

pub enum C {
    C1,
    C2,
}

const A: &[C] = [C::C1, C::C2].as_slice();
const B: &[C] = [C::C1].as_slice();

pub enum D {
    D1 = A,
    D2 = B,
}

Instead one must implement a get_value() function like so:

impl D {
    pub fn get_value(&self) -> &[C] {
        match self {
            D1 => A,
            D2 => B,
        }
    }
}

I have an example in my own code as well. See for more discussion: https://stackoverflow.com/questions/36928569/how-can-i-create-enums-with-constant-values-in-rust

Thanks!

kimono-koans avatar Aug 14 '22 19:08 kimono-koans