Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Funny enough, manually defining the discriminant disables the enumerator:

    enum Discriminant1 {
        Disc0,
        Disc1,
    }

    #[repr(u8)]
    enum Discriminant2 {
        Disc0 = 10,
        Disc1 = 20
    }

    fn main() {
        let d1 = Discriminant1::Disc1;
        let d2 = Discriminant2::Disc1;
        println!("{:?}", std::mem::discriminant(&d1)); // Value by enumerator.
        println!("{:?}", std::mem::discriminant(&d2)); // Value by constant.
    }
Which makes the use of the enum keyword particularly bizarre given that there is no longer even an enumerator involved, but I suppose bizarre inconsistencies are par for the course in Rust.


What's that have to do with Rust though? Rust takes it straight from C: https://godbolt.org/z/Ysb6M66h4

And because it has been used like that in C for decades, the dictionary definition takes a backseat to the now de-facto C-based definition (at least for popular systems languages, which Rust is trying to share as much syntax with).


> Rust takes it straight from C

Meaning the keyword? Sure, C has the same inconsistency if you disable the enumerator with manual constant values. C is not exactly the paragon of thoughtful design. But whataboutism is a dumb path to go down.

> the dictionary definition takes a backseat to the now de-facto C-based definition

That's clearly not the case, though, as the functionality offered by the Rust enum keyword is very different. It puts absolutely no effort into being anything like C. Instead, it uses enum as the keyword for defining sum types. The C enum keyword, on the other hand, does nothing but define constants, and is functionally identical to what Go has. There is an enum involved in both cases, as demonstrated earlier, so the terminology isn't strictly wrong (in the usual case) but the reason for it existing shares little commonality.

But maybe you've moved onto the concept of enums rather than syntax and I didn't notice? You are right that the dictionary definition is in line with the intent of the C keyword, which speaks to the implementation, and is how C, Rust, Go, and every other language out there use the terminology. In another comment I even linked to the implementation in both Go and Rust and you can see that the implementation is conceptually the same in both cases: https://news.ycombinator.com/item?id=44236666




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: