News for hackers. Stuff that matters.
Re:Using enums to represent state in Rust by flohofwoe on Friday September 22, 2023 @10:02AM (#37612189)
I just wish Rust wouldn't have called those things 'enums' but 'tagged unions', would have saved us C peasants a lot of confusion when encountering them first ;)
Re:Using enums to represent state in Rust by joshmarlow on Friday September 22, 2023 @10:14AM (#37612343)
C was my first language, and I actually have the opposite opinion - I suspect that calling them 'enums' helps adoption. My reasoning is that phrases like 'tagged unions' and 'algebraic datatypes' strike some developers as sounding very academic/ivory tower and so somewhat intimidating. I think this is really unfortunate (and is definitely not universal among developers).

Being able to say 'Rust enums can carry arguments' - for some reason - sounds less intimidating and conveys the core feature.

Re:Using enums to represent state in Rust by flohofwoe on Friday September 22, 2023 @10:17AM (#37612375)
I agree that something like 'sum type' sounds a bit too esoteric, but IMHO 'tagged union' is very descriptive. It's the same thing as a C union plus a tag indicating the currently active content (and that's also what the memory layout looks like under the hood).

(but yeah, 'enum with arguments' also describes it very well)

Re:Using enums to represent state in Rust by LoganDark on Friday September 22, 2023 @11:49AM (#37613570)
They do point them out as tagged unions many times in the documentation. But the syntax `tagged_union Whatever {}` doesn't exactly have me jumping out of my seat.
Re:Using enums to represent state in Rust by pavlov on Friday September 22, 2023 @02:33PM (#37615847)
It’s not like “enum” and “mut” are complete English words either, so maybe an abbreviation like “tun” could have worked.

But honestly I think enum was a better choice.

Re:Using enums to represent state in Rust by LoganDark on Friday September 22, 2023 @04:25PM (#37617308)
I personally think enum was basically the only reasonable choice, as normal enums really are just a subset of tagged unions. They just happen to support tagged unions on top of them.
Re:Using enums to represent state in Rust by goku12 on Friday September 22, 2023 @10:23AM (#37612461)
All C and C++ peasants should refer this: https://cheats.rs/#memory-layout . It will probably accelerate your initiation ritual.
Re:Using enums to represent state in Rust by Arnavion on Friday September 22, 2023 @12:04PM (#37613771)
That would still be technically misleading, since enums don't necessarily have tags due to niche optimization. Eg `Option` is an enum but `Option<Box<Foo>>` does not have any tags; it uses the content being zero to represent None since `Box<Foo>` cannot be zero.

Also, Rust does have actual tagged unions for C interop that you have to define yourself as a `struct` with an int field and a `union` field, just like in C.

Re:Using enums to represent state in Rust by twic on Friday September 22, 2023 @01:43PM (#37615126)
Rust enums always have a discriminant: https://doc.rust-lang.org/std/mem/fn.discriminant.html

Their representation in memory may not use it, but it's still defined.

Re:Using enums to represent state in Rust by 0x457 on Friday September 22, 2023 @02:37PM (#37615902)
They do, but IIRC this mostly to compare enum variants when it can't implement `Eq` and/or `PartialEq`. For example, in tests.

There are also zero variants enums that don't have any discriminant, but still could be used.

I'm not sure what's the point of comparing it to C because enums in C only carry tags and no data, while unions only carry data and no tag. Enums in rust could do both.

Re:Using enums to represent state in Rust by touisteur on Friday September 22, 2023 @03:57PM (#37616957)
Discriminated Records is The Way.