Unit structs
A unit struct has a name and no fields: struct Logout;. It is a zero-sized type, useful as a marker, a trait implementor, or a signal value.
Detailed Explanation
struct Logout; declares a type with no data. The value is written Logout, the same as the type name. It occupies zero bytes. A Vec<Logout> of a million elements still has a zero-byte allocation for the elements; only the Vec header exists. The compiler treats the value as a compile-time tag.
Why have a type with no data? Because traits attach to types, not to values. You want a type that implements Error for a specific failure, or Iterator for an empty sequence, or a marker that shows up in a generic parameter. struct FileNotFound; plus impl std::fmt::Display for FileNotFound is an error type with no extra payload. struct AlreadyLoggedIn; can be a Result error variant's meaning without a string allocation.
Unit structs are also the unit variants of the idea that enums have: enum Event { Logout, Login(UserId) } has a unit variant Logout. A standalone unit struct is what you write when that variant wants to be its own type — for example so it can implement a trait without the rest of the enum.
They appear in typestate and marker APIs. struct Open; struct Closed; as generic parameters on Connection<State> let the compiler refuse connection.read() when State is Closed. The state is not stored at runtime; it lives in the type. That only works because Open and Closed are distinct types with no data to keep in sync.
A unit struct is not the unit type. The unit type is (), the empty tuple, and there is only one. Every unit struct you declare is a new type, incompatible with () and with the others. struct A; struct B; cannot be substituted for each other.
When you reach for a unit struct: a marker, a zero-payload error, a typestate tag, or a trait impl that needs a type and no storage. When you need even one byte of data, a tuple struct or a named-field struct. When you need several named cases in one type, an enum of unit variants.
Code Examples
struct FileNotFound;
impl std::fmt::Display for FileNotFound {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "file not found")
}
}
fn main() {
let err = FileNotFound;
println!("{err}"); // prints: file not found
println!("{}", std::mem::size_of_val(&err)); // prints: 0
}Common Gotchas
A unit struct is not `()`
`struct Done;` and `()` are different types. A function that returns `()` cannot return `Done` without a conversion you write.
Zero size still has alignment 1
You can take a reference to a unit struct. The address may be dangling-looking (often `1` as a non-null sentinel) and is not a real allocation.
A million ZSTs in a `Vec` can still overflow `len`
The elements take no heap, but `len` and `capacity` are `usize`. The interesting cost is time spent in the loop, not memory.
Unit variants on enums are usually enough
Declare a standalone unit struct when you need a type of its own, not only a case of a larger enum.