How can I create enums with constant values in Rust?

The enum-map crate provides the ability to assign a value to the enum record. What is more, you can use this macro with different value types.

use enum_map::{enum_map, Enum}; // 0.6.2

#[derive(Debug, Enum)]
enum Example {
    A,
    B,
    C,
}

fn main() {
    let mut map = enum_map! {
        Example::A => 1,
        Example::B => 2,
        Example::C => 3,
    };
    map[Example::C] = 4;

    assert_eq!(map[Example::A], 1);

    for (key, &value) in &map {
        println!("{:?} has {} as value.", key, value);
    }
}

Creating an "enum" with constant values, can be augmented using structs and associated constants. This is similar to how crates like bitflags works and what it would generate.

Additionally, to prevent direct instantiation of MyEnum you can tag it with #[non_exhaustive].

#[non_exhaustive]
struct MyEnum;

impl MyEnum {
    pub const A: i32 = 123;
    pub const B: i32 = 456;
}

Then you simply use the "enum" as you otherwise would, by accessing MyEnum::A and MyEnum::B.


People looking at this may stumble upon the introduction and deprecation of FromPrimitive. A possible replacement which might also be useful here is enum_primitive. It allows you to use C-like enums and have them cast between numeric and logical representation:

#[macro_use]
extern crate enum_primitive;
extern crate num;

use num::FromPrimitive;

enum_from_primitive! {
    #[derive(Debug, PartialEq)]
    enum FooBar {
        Foo = 17,
        Bar = 42,
        Baz,
    }
}

fn main() {
    assert_eq!(FooBar::from_i32(17), Some(FooBar::Foo));
    assert_eq!(FooBar::from_i32(42), Some(FooBar::Bar));
    assert_eq!(FooBar::from_i32(43), Some(FooBar::Baz));
    assert_eq!(FooBar::from_i32(91), None);
}

The best way to answer this is working out why you want constants in an enum: are you associating a value with each variant, or do you want each variant to be that value (like an enum in C or C++)?

For the first case, it probably makes more sense to just leave the enum variants with no data, and make a function:

enum MyEnum {
    A,
    B,
}

impl MyEnum {
    fn value(&self) -> i32 {
        match *self {
            MyEnum::A => 123,
            MyEnum::B => 456,
        }
    }
}
// call like some_myenum_value.value()

This approach can be applied many times, to associate many separate pieces of information with each variant, e.g. maybe you want a .name() -> &'static str method too. In the future, these functions can even be marked as const functions.

For the second case, you can assign explicit integer tag values, just like C/C++:

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

This can be matched on in all the same ways, but can also be cast to an integer MyEnum::A as i32. (Note that computations like MyEnum::A | MyEnum::B are not automatically legal in Rust: enums have specific values, they're not bit-flags.)

Tags:

Rust