The unit type in Rust, denoted byDocumentation Index
Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt
Use this file to discover all available pages before exploring further.
(), is a type that possesses exactly one possible value, which is also written as (). In type theory, it is the equivalent of a terminal object; it represents the absence of meaningful data while remaining a concrete, fully-fledged type within Rust’s type system.
Unlike void in C or C++, which signifies the absence of a type, () is a valid type that can be instantiated, assigned to variables, and returned from functions.
Memory Layout
Because the unit type has only one possible state, it carries no information. Consequently, the Rust compiler treats it as a Zero-Sized Type (ZST). It occupies exactly zero bytes of memory at runtime.[(); N]) consume zero bytes of stack space. For dynamic collections like Vec<()>, the collection will never allocate heap memory for its buffer regardless of the number of elements it holds. However, the collection struct itself still consumes stack space for its internal metadata (such as its pointer, capacity, and length fields).
Syntax and Evaluation
The() token serves a dual purpose: it acts as both the type signature and the value itself.
;), the expression is evaluated, its actual result is discarded, and the statement evaluates to ().
Function Signatures
Functions in Rust that do not declare an explicit return type implicitly return the unit type. The following two function signatures and their bodies are semantically identical to the compiler:Master Rust with Deep Grasping Methodology!Learn More





