Live data from Hacker News

Using enums to represent state in Rust

corrode.dev

11–20 of 89 posts

Re: Using enums to represent state in Rust

#11
post #4

I kind of consider algebraic data types (the combination of this style of enum (sum types) and structs (product types)) paired with pattern matching as the core feature set that any modern language must support. It makes for really simple, easy to follow code that’s much less error prone than the alternatives.

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

#12
Rust ADTs and pattern matching are so much better than other mainstream languages I find that once my code compiles it actually is almost always correct.

The next step is to encode your transition logic in the From impls between the enum structs and you've got yourself a first-rate state machine.

Re: Using enums to represent state in Rust

#15
post #13

> Deleted { deleted_at: DateTime }, What does this look like under the hood (in memory)? Does the compiler automatically generate a struct/union? Does the value take up the same width regardless of state?

Yes; they take up the size of the largest variant. If you're concerned about that, you can Box the contents, and then each variant is effectively a same-sized pointer to something on the heap (plus some bookkeeping).

Re: Using enums to represent state in Rust

#16
Perhaps I am being cynical, but this doesn't really provide a better explanation about Rust's ADT enums than the official Rust book, albeit with the note about `#[repr(...)]`. Seems a bit low effort, I would have liked to have seen some examples that are more practical and less foobar-ish.

Re: Using enums to represent state in Rust

#17
post #4

I kind of consider algebraic data types (the combination of this style of enum (sum types) and structs (product types)) paired with pattern matching as the core feature set that any modern language must support. It makes for really simple, easy to follow code that’s much less error prone than the alternatives.

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 ;)

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

#18

Earlier quoted context omitted.

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 ;)

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 argume…

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

#19
post #12

Rust ADTs and pattern matching are so much better than other mainstream languages I find that once my code compiles it actually is almost always correct. The next step is to encode your transition logic in the From impls between the enum structs and you've got yourself a first-rate state machine.

> Rust ADTs and pattern matching are so much better than other mainstream languages

Pascal and Delphi have always had variant records as part of the language. Are these not "mainstream" enough, especially Delphi?

Re: Using enums to represent state in Rust

#20
post #4

I kind of consider algebraic data types (the combination of this style of enum (sum types) and structs (product types)) paired with pattern matching as the core feature set that any modern language must support. It makes for really simple, easy to follow code that’s much less error prone than the alternatives.

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 ;)

All C and C++ peasants should refer this: https://cheats.rs/#memory-layout . It will probably accelerate your initiation ritual.
Post reply on HN