vnum

Create enums with a constant value associated to every variant.

Features

Examples

```rust value_enum! { enum Fruit: &'static str { Apple = "red", Banana = "yellow", Pear = "green" } }

// Get the value with the .value() method: let apple = Fruit::Apple; println!("Apple: {}", apple.value()); // Apple: red

// Get the value with the From / Into traits: let pear = Fruit::Pear; let value: &str = pear.into(); println!("Pear: {}", value); // Pear: green ```

Note: The type of the values must be specified after the enum name, just like above (&'static str in this case).

Note: If the value type is a reference (&) or contains references, the 'static lifetime must be used,\ otherwise the Rust compiler would not know where the value is borrowed from.


```rust value_enum! { #[derive(Debug)] enum Color: u8 { Red = 1, Green = 2, Yellow = 3 } }

let red = Color::Red; println!("{:?}: {}", red, red.value()); // Red: 1

let yellow = Color::Yellow; let value: u8 = yellow.into(); println!("Yellow: {}", value); // Yellow: 3 ```

Note: If you want more traits implemented for your enum, you have to do it yourself.\ In the example above, the Debug trait is derived.

Note: Only constant expressions are allowed to the right of the equals sign,\ which means they must be evaluable at compile time.\ Look here for all kinds of constant expressions: https://doc.rust-lang.org/reference/const_eval.html#constant-expressions

Alternatives


Planned features


License

Licensed under either of

at your option.

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.