How to create enums with constant values in Rust?
Hello colleagues! I started learning Rust after C++ and Python, and encountered an unexpected problem: how to properly create enums with constant values? In C++ I used to write something like `enum Color { RED = 0xFF0000, GREEN = 0x00FF00, BLUE = 0x0000FF };`, and each variant had a numeric value. In Python I used `IntEnum` or just classes with constants. But in Rust, when I try to do this:
```rust
enum HttpStatus {
Ok = 200,
NotFound = 404,
InternalServerError = 500,
}
```
The compiler gives an error if I try to use these values as integers. For example, I want to convert the status to a number for sending over the network or for logging. I tried writing `status as u32`, but this only works for enums that explicitly inhe
No answers yet.
Log in to leave an answer
Blogs