Zero-sized type

From Rust Community Wiki
Jump to navigation Jump to search

A type that occupies no memory and is optimized away by the compiler. For example, a Vec<()> never allocates memory. It is usually abbreviated ZST.

Zero-sized types are:

  • The empty tuple ()
  • The never type !
  • Structs, unions and tuples, if all their fields are zero-sized, e.g.
    struct Struct;
    struct Struct2(Struct);
    struct Struct3(PhantomData<bool>);
  • Enums with at most one variant that isn't uninhabited, if all its fields are zero-sized, e.g.
    enum Void {}
    enum Unit { Unit }
    enum Unit2 {
    enum Phantom {

Note that PhantomDataThis links to official Rust documentation is also a ZST. It is a special type, because it is generic, even though it has no fields.

Note that struct Foo(i32, !) and enum Foo { Foo(i32, !) } are not zero-sized. Rust doesn't treat them as uninhabited types, even though they have no possible values.