> ## 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.

# Rust Unit Variant

A unit variant is a specific type of `enum` variant in Rust that contains no associated data or payload. Structurally identical to a unit struct, it consists solely of an identifier and represents a discrete, singular state within an enumeration. Because it holds no underlying data, a unit variant is isomorphic to the unit type `()` in terms of data capacity.

## Syntax and Instantiation

Unit variants are declared using only their name, without parentheses `()` or curly braces `{}`. They are instantiated using the path separator `::`.

```rust theme={"dark"}
enum ProcessState {
    Halted,   // Unit variant
    Running,  // Unit variant
    Faulted,  // Unit variant
}

// Instantiation requires no arguments
let current_state = ProcessState::Running;
```

## Memory Layout and Discriminants

To differentiate between enum variants at runtime, the Rust compiler uses a hidden integer tag known as a **discriminant**. This mechanism applies to *all* enum variants, regardless of whether they carry a payload. For a unit variant, the discriminant is the only data required to identify the variant's state.

By default, the Rust compiler assigns discriminants starting at `0` and incrementing by `1` for each subsequent variant. However, you can explicitly assign integer values to unit variants.

```rust theme={"dark"}
enum HttpStatusCode {
    Ok = 200,           // Explicit discriminant
    BadRequest = 400,   // Explicit discriminant
    Unauthorized = 401, // Explicit discriminant
    PaymentRequired,    // Implicitly assigned 402
}
```

If an `enum` consists *entirely* of unit variants, it is known as a "fieldless enum" or "C-like enum". You can cast a fieldless enum directly to an integer type using the `as` keyword to extract its discriminant:

```rust theme={"dark"}
let code = HttpStatusCode::BadRequest;
let numeric_code = code as i32; // Evaluates to 400
```

## Pattern Matching

When evaluating a unit variant in a `match` expression or `if let` binding, you match against the exact identifier. Since there is no inner data to destructure, no variable binding occurs during the match.

```rust theme={"dark"}
let state = ProcessState::Halted;

match state {
    ProcessState::Halted => {
        // Matched exactly; no payload to extract
    }
    ProcessState::Running => {}
    ProcessState::Faulted => {}
}
```

## Contrast with Other Variants and Memory Footprint

To understand unit variants technically, it is helpful to contrast them with the other two types of enum variants in Rust:

```rust theme={"dark"}
enum VariantTypes {
    UnitVariant,                             // No payload
    TupleVariant(i32, String),               // Anonymous positional payload
    StructVariant { id: i32, name: String }, // Named payload fields
}
```

Rust enums are conceptually implemented as tagged unions. This means the memory footprint of an enum instance is dictated by its largest variant. Consequently, an instance of `UnitVariant` occupies the exact same amount of memory as an instance of `StructVariant` or `TupleVariant` within the `VariantTypes` enum.

However, the total size of an enum is not strictly the size of the largest payload plus an explicit discriminant. Rust performs **niche filling optimizations** (such as the null pointer optimization). If the payload of a variant contains invalid bit patterns (niches)—such as `NonZeroU32`, references `&T`, or `bool`—the compiler can encode the discriminant of the unit variants within those invalid bit patterns. In such cases, the enum requires no extra memory for the discriminant, and its total size is exactly equal to the size of the payload alone. If no niches are available, the enum size will equal the largest payload plus the size of the discriminant and any necessary alignment padding.

<div
  style={{ 
display: "flex", 
justifyContent: "space-between", 
alignItems: "center", 
maxWidth: "754px", 
padding: "1rem 0",
marginBottom: "24px"
}}
>
  <span style={{ fontWeight: "bold", fontSize: "1.25rem", color: "var(--tw-prose-headings)", fontFamily: "Inter, ui-sans-serif, system-ui, sans-serif" }}>Tired of Poor Rust Skills? Fix That With Deep Grasping!</span>

  <a
    href="https://syntblaze.com"
    target="_blank"
    style={{ 
  marginLeft: "24px",
  textDecoration: "none", 
  backgroundColor: "#007AFF",
  color: "#ffffff", 
  padding: "6px 16px", 
  borderRadius: "16px",
  fontSize: "0.9rem",
  fontWeight: "600",
  textAlign: "center",
  transition: "background-color 0.2s ease"
}}
  >
    Learn More
  </a>
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/skill-tracking.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=b9b0305c93bb501c9e767b5c76c88835" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/skill-tracking.png" />

  <img src="https://mintcdn.com/syntblazellc/23tyuOzaWS88qFlc/images/nuggets.png?fit=max&auto=format&n=23tyuOzaWS88qFlc&q=85&s=c86c80197299762989e9b882419b2109" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/nuggets.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/bite-sized-exercises.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=a65f9a38c37ff28ab73ed783c53c60e3" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/bite-sized-exercises.png" />
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap", marginTop: "12px" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/mastery-chain.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=748a1763454713e679260fbb95f154a2" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/mastery-chain.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-previews.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=242f61448ff5dd6deaaab2dccc13b507" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-previews.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-explanations.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=cf0fc1c31f9cd0fc26716781be05fbc9" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-explanations.png" />
</div>
