An enum (enumeration) in Rust is an algebraic data type—specifically a sum type or tagged union—that allows a value to be exactly one of a predefined set of mutually exclusive variants. Unlike enums in languages like C or Java, Rust enums can encapsulate heterogeneous data payloads directly within their variants, making them highly expressive for state representation and type-safe data encapsulation.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.
Variant Types and Type Identity
Rust supports three distinct types of enum variants, which can be mixed within a single enum declaration:- Unit-like variants: Contain no data.
- Tuple-like variants: Contain unnamed, ordered fields.
- Struct-like variants: Contain named fields.
Message). A function cannot accept a specific variant (e.g., Message::Write) as a parameter type or variable type signature; it must accept the parent Message type.
Instantiation
Instances of an enum are created by scoping the variant under the enum’s namespace using the path separator (::). The syntax for instantiation mirrors the syntax of the variant’s declaration.
Methods and Associated Functions
Enums in Rust possess object-like capabilities. You can define methods and associated functions on them usingimpl blocks, allowing behavior to be tightly coupled with the algebraic data type.
Pattern Matching and Destructuring
Because enum variants can hold data, accessing that data requires pattern matching. Rust enforces exhaustiveness checking, meaning every possible variant must be accounted for at compile time. Data is extracted from variants via destructuring inmatch expressions or if let bindings.
When matching on an enum that contains non-Copy data (like String), the match expression will consume the value unless it is borrowed.
if let provides a more concise syntax for destructuring without requiring exhaustive match arms. Because msg was borrowed in the previous example, it remains valid for subsequent use:
Memory Layout
Under the hood, a Rust enum is typically implemented as a tagged union. The compiler allocates memory based on two components:- The Discriminant (Tag): A hidden integer used by the compiler to track which variant is currently active.
- The Payload: The actual data contained within the variant.
&T, or the value 0 for std::num::NonZeroU8)—the compiler can store the enum’s discriminant inside those invalid bit patterns. This eliminates the need for a separate discriminant in memory. Because of this optimization, an enum like Option<&T> has the exact same memory footprint as a raw pointer &T, with zero extra bytes added for the enum tag.
Explicit Discriminants
For unit-only enums (often called C-like enums), you can explicitly define the discriminant values. This is primarily used for FFI (Foreign Function Interface) or when casting to primitive integer types. You can control the memory representation using the#[repr] attribute. The chosen representation must be large enough to hold the assigned values.
Generic Enums
Enums can be parameterized over types and lifetimes. This allows the definition of abstract sum types that operate on arbitrary data.Master Rust with Deep Grasping Methodology!Learn More





