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-dev mailing list
[email protected]
https://mail.mozilla.org/listinfo/rust-dev

Reply via email to