2 ms·
I, uh, don't really follow that logic. A Rust enum isn't even "quite close" to a C enum, yet you used a colliding term. Surely a C programmer who would have b
by ajross 12y ago
I, uh, don't really follow that logic. A Rust enum isn't even "quite close" to a C enum, yet you used a colliding term. Surely a C programmer who would have been mislead about the fact that a union type is runtime-tagged and checked is going to be very misled that an "enum" has no numeric value and can contain variable state at runtime.
- steveklabnik 12y agoI guess my ultimate point is that _everything_ is a colliding term in some way, with some language. Rust isn't C, so you can't assume that things map 1:1 to C. Names are hard.
- deleted 12y ago[deleted]
- lambda 12y agoA Rust enum does does carry a numeric value, if it doesn't contain any other data (playpen link: http://is.gd/HwGajU http://is.gd/HwGajU) enum Foo { Bar, Baz, Quux, Zot = -1 } fn main() { println!("{} {} {} {}", Foo::Bar as i32, Foo::Baz as i32, Foo::Quux as i32, Foo::Zot as i32); } A Rust enum is pretty much an exact combination of a C enum and union. If it doesn't carry any extra data, then it works just like an enum; if it does carry extra data, it works like a tagged union.
- kibwen 12y ago> A Rust enum isn't even "quite close" to a C enum This is incorrect. The following Rust enum compiles down to a single byte, whose variants are represented by the numbers 0, 1, and 2: enum Foo { Zero, One, Two } You can even give them all numeric values explicitly: enum Bar { Ten = 10, Eighty = 80, TwoHundred = 200 } And you can also just tell it where to start and let it count from there: enum Qux { Five = 5, Six, Seven } If you throw the #[repr(C)] attribute on any of these then Rust will make sure to size them as C would on your particular platform (on my machine this attribute inflates them from 8 bits to 64 bits), making them usable directly from C as well.