A unit struct is a struct defined without any fields, terminating with a semicolon instead of curly braces. In Rust’s type system, it is classified as a Zero-Sized Type (ZST). Because it contains no data, the struct itself occupies exactly zero bytes of memory at runtime, and the compiler optimizes away any runtime overhead associated with its instantiation, copying, or moving.Documentation Index
Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt
Use this file to discover all available pages before exploring further.
Syntax and Instantiation
A unit struct is defined using thestruct keyword followed by the identifier and a semicolon. It is instantiated using only its identifier, without parentheses or curly braces.
Memory Characteristics
Because a unit struct lacks fields, the Rust compiler does not allocate memory for the value itself. You can verify this usingstd::mem::size_of.
Method and Trait Implementation
Despite having no internal state, unit structs are fully qualified types. You can implement inherent methods and traits for them using standardimpl blocks.
self parameter is passed as a zero-sized value. If the method takes a reference (&self), the reference itself is a standard pointer and occupies pointer-sized memory (e.g., 8 bytes on a 64-bit architecture), but it points to a zero-sized value. Regardless of how it is passed, the compiler resolves the method call statically without needing to dereference a memory address to read internal state.
Pattern Matching and Destructuring
Unit structs can be matched inmatch expressions. Because they have only one possible value (the unit itself), matching against them is exhaustive by default.
Deriving Traits
Unit structs support the#[derive(...)] attribute. Standard library traits like Debug, Clone, Copy, PartialEq, and Eq can be automatically derived. Because there are no fields to compare or copy, these derived implementations are trivial and incur zero runtime cost.
Master Rust with Deep Grasping Methodology!Learn More





