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

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.

## Syntax and Instantiation

A unit struct is defined using the `struct` keyword followed by the identifier and a semicolon. It is instantiated using only its identifier, without parentheses or curly braces.

```rust theme={"dark"}
// Definition
struct UnitStruct;

fn main() {
    // Instantiation
    let instance = UnitStruct;
}
```

## Memory Characteristics

Because a unit struct lacks fields, the Rust compiler does not allocate memory for the value itself. You can verify this using `std::mem::size_of`.

```rust theme={"dark"}
use std::mem;

struct UnitStruct;

fn main() {
    assert_eq!(mem::size_of::<UnitStruct>(), 0);
}
```

When a unit struct is passed to a function or returned by value, no actual data is copied or moved at the machine-code level. The type exists purely at compile time for type checking and dispatch.

## 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 standard `impl` blocks.

```rust theme={"dark"}
struct UnitStruct;

// Inherent methods
impl UnitStruct {
    fn execute(&self) {
        println!("Executing method on a ZST.");
    }
}

// Trait implementation
trait Processor {
    fn process(&self);
}

impl Processor for UnitStruct {
    fn process(&self) {
        println!("Processing via trait.");
    }
}
```

When methods are called on a unit struct, the `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 in `match` expressions. Because they have only one possible value (the unit itself), matching against them is exhaustive by default.

```rust theme={"dark"}
struct UnitStruct;

fn evaluate(val: UnitStruct) {
    match val {
        UnitStruct => println!("Matched the unit struct exactly."),
    }
}
```

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

```rust theme={"dark"}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
struct UnitStruct;

fn main() {
    let a = UnitStruct;
    let b = a; // Implicitly copied due to `Copy`
    
    assert_eq!(a, b); // Evaluates to true via `PartialEq`
}
```

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