On Sun, May 5, 2013 at 10:21 PM, NAKASHIMA, Makoto <[email protected]> wrote: > Hello, > > I'm new to this list and I enjoy programming in Rust. > > I have a question. > Does rust's enum type support specifying base type? > > C++11 and D support it as below. > > // C++11 and D > enum Color: uint { RED = 0xff0000, GREEN = 0x00ff00, BLUE = 0x0000ff } > > // D only > enum Foo: string { AAA = "aaa", BBB = "bbb", CCC = "ccc" } > > > I tried using this in rust, but it seems not supported (Code 1 and Code 2). > > * Code 1 > enum Color: uint { RED = 0xff0000, GREEN = 0x00ff00, BLUE = 0x0000ff } > > enum.rs:1:10: 1:11 error: expected `{` but found `:` > enum.rs:1 enum Color: uint { RED = 0xff0000, GREEN = 0x00ff00, BLUE = > 0x0000ff } > > * Code 2 > enum Color { RED = 0xff0000u, GREEN = 0x00ff00u, BLUE = 0x0000ffu } > > enum.rs:1:19: 1:28 error: mismatched types: expected `int` but found > `uint` (expected int but found uint) > enum.rs:1 enum Color { RED = 0xff0000u, GREEN = 0x00ff00u, BLUE = > 0x0000ffu } > > > I think enum with base type is very usable for bit field flags or masks. > > Thank you. > > gifnksm
Rust's enums aren't usable for bit fields or masks because they're guaranteed to only contain the variants you specify. It would be better to make a type with the | operator overloaded and pre-defined statics. They're a tagged union rather than what C/C++/D call an enum. I think the naming is unfortunate. Probably too late to call them "variant" though :P. _______________________________________________ Rust-dev mailing list [email protected] https://mail.mozilla.org/listinfo/rust-dev
