It is possible to write constructions like this:
enum Number {
One = 1,
Two = 2,
Three = 3,
Four = 4,
}
but for what purpose? I can't find any method to get the value of an enum variant.
You get the value by casting the enum variant to an integral type:
enum Thing {
A = 1,
B = 2,
}
fn main() {
println!("{}", Thing::A as u8);
println!("{}", Thing::B as u8);
}
struct Piece(u8)
which is the packed form, and two enums PieceType { A, B, C }
and Color { Red, White }
. The Piece
constructor would take in an Option<PieceType>, Color
and Piece
would have methods to convert to and from the packed form. Your Piece
could even use the bitflags mentioned in a comment above.
Commented
Jan 22, 2018 at 22:11