DEV Community

Discussion on: Rust - First Impressions

Collapse
 
armousness profile image
Sean Williams

As the name implies, you can implement them yourself in C/C++. It basically consists of three things: an enum that lists out the constructors (Type1, Type2, and Type 3, in OP's example); a union over the types being summed (String, u32, and unit); and a struct containing the enum and the union. You then switch the enum to figure out what the underlying type is, and access the associated union case to get the data.

This is why they're called tagged unions or discriminated unions.