I can think of many places where unions in C help are useful and they save memory. As Rust is a system programming language, why doesn\'t it support unions?
Unions were added to the language in (RFC 1444), and they are stable as of Rust 1.19.0. They require usage of unsafe
blocks.
Raw unions are not memory-safe (as there is no way for the compiler to guarantee that you always read the correct type (that is, the most recently written type) from the union). One of the goals of Rust is to create a low-level language with memory safety; since unions are not compatible with that goal, they were not included in Rust 1.0.
Instead, Rust has enums, which provide most of the advantages of unions in exchange for a small memory usage, but which are memory safe since the enumeration value always keeps track of which particular type it contains.
Rust has tagged unions in the form of its algebraic data types, enum
:
enum Foo {
Bar(i32),
Baz,
Quux {
misc: A,
ellaneous: B,
fields: C,
},
}
A Foo
there can be either a Bar
with an attached i32
, a Baz
with no additional data or a Quux
with those three miscellaneous fields. This is a tagged union—the size of an enum will not exceed the largest of its variants plus as much as is needed for the tag (typically one byte, but I guess it’s possible to have more variants than fit in one byte), and in certain cases where it can be optimised (like Option<&T>
where a memory address of 0 is not legal for the Some
variant and so can be used to represent the None
variant) the variant is squeezed into the value.
What Rust does not have is untagged unions as in C. Why? Because they’re fundamentally unsafe, and safety is paramount for Rust. If you still want something like that, it’s perfectly possible to create a wrapper around the unsafe code that you will wind up with with things like transmutation happening, but you simply don’t need untagged unions in normal life.
Rust does now support untagged unions as an unsafe concept; as of 1.19.0.