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?
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.