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

You seem to be confused which is understandable. By tagged unions people mean the "enum" data type.

Rust enums are a little different than in other languages that you are likely used to.

They can act like the classic C enums that you are probably used to, where they are more or less just named constants grouped together under a single type.

Then, unlike the classic C style enum, they can also hold data in them, where each variant is basically a struct with named fields and everything. They also support methods just like structs do (the methods are per enum and not per variant). In cases where you don't care about the fields names, you can define the variant as a tuple and later access fields by index.

Struct/Tuple enums are actually implemented as tagged unions by the compiler but this is completely invisible to you so you don't really need to care about it!

Enums are also totally "safe" to create and use!

Rust also has a union type which is like a C union, and you could use it to implement tagged unions (requires unsafe) if you wanted to, but in almost all cases you will just use "enum" for this purpose.

Overloading the "enum" into two different things seems like a strange choice to me but I don't know what the history is behind it, and it takes just a few minutes to figure it out so it's not a huge deal!



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

Search: