On Sun, May 5, 2013 at 10:21 PM, NAKASHIMA, Makoto
<makoto.n...@gmail.com> 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?

> 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 }

Quoting from the section of the tutorial on enums
(http://static.rust-lang.org/doc/tutorial.html#enums):

"When an enum is C-like (that is, when none of the variants have
parameters), it is possible to explicitly set the discriminator values
to a constant value:

enum Color {
  Red = 0xff0000,
  Green = 0x00ff00,
  Blue = 0x0000ff
}"

So, in other words, what you did almost works, but you should leave
the `u` suffixes off of the base values because they should be ints,
not uints -- which is why you got the error message you got.  Here's a
little example program:

fn main() {
   enum Color { RED = 0xff0000, GREEN = 0x00ff00, BLUE = 0x0000ff }
   print(fmt!("%d\n", BLUE as int)); // prints 255
}

Lindsey
_______________________________________________
Rust-dev mailing list
Rust-dev@mozilla.org
https://mail.mozilla.org/listinfo/rust-dev

Reply via email to