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

Close, but not quite: unless the type is NonZero, you need space for the tag.

c-style unions are in nightly behind a flag; they're not stable yet.



I think they meant that the use case corresponds closely, not the representation. Generally C unions get used the same place Rust unions would in a rust program (not vice versa though), unless they're being used for type punning, which is pretty rare anyway.


I meant if you have a large program where many types are C unions like:

    struct TaggedUnion {
        enum {T1, T2} type_id;
        union {type1 t1; type2 t2;} u;
    }
If the goal is to translate automatically to a rust enum declaration, that will be (a) possible to do with an automatic tool and (b) will give enhanced safety because it will force the type to be checked everywhere these values are used.




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

Search: