Reputation: 16815
Is there a way I can make a C++ style enumeration with explicit representation type in Rust? Example:
enum class Number: int16_t {
Zero, One, Two, Three, Four, Five, Six, Seven, Eight, Nine
};
If not, is there another way I can organize variables like that? I am interfacing with an external library, so specifying the type is important. I know I could just do:
type Number = int16_t;
let One: Number = 1;
let Two: Number = 2;
let Three: Number = 3;
But that introduces a lot of redundancy, in my opinion;
Note this question is not a duplicate of Is it possible to wrap C enums in Rust? as it is about wrapping C++, not wrapping C.
Upvotes: 6
Views: 2616
Reputation: 16630
You can specify a representation for the enum.
#[repr(i16)]
enum Foo {
One = 1,
Two = 2,
}
Upvotes: 16