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

A generic struct in Rust is a composite data type parameterized over one or more types. By declaring type parameters within angle brackets (`<T>`) immediately following the struct name, the struct defers the concrete type resolution of its fields until instantiation. The Rust compiler resolves these generics at compile time through a process called monomorphization, generating specific, statically-typed implementations for each concrete type used, ensuring zero runtime overhead.

## Syntax and Declaration

Type parameters are declared in the struct signature and then applied to the types of the internal fields.

```rust theme={"dark"}
struct Point<T> {
    x: T,
    y: T,
}
```

In this definition, `T` represents a single concrete type. Because both `x` and `y` are typed as `T`, they must be instantiated with the exact same type. Attempting to mix types (e.g., assigning an `i32` to `x` and an `f64` to `y`) will result in a compiler error.

To allow fields to hold different types, multiple type parameters must be declared:

```rust theme={"dark"}
struct Pair<T, U> {
    first: T,
    second: U,
}
```

## Implementation Blocks (`impl`)

When defining methods for a generic struct, the type parameters must be declared on the `impl` block before they can be applied to the struct name. This tells the compiler that the `impl` block is generic over `T`.

```rust theme={"dark"}
impl<T> Point<T> {
    fn x(&self) -> &T {
        &self.x
    }
}
```

You can also write implementation blocks targeting a specific concrete type. Methods defined in this block will only be available to instances of the struct instantiated with that exact type.

```rust theme={"dark"}
impl Point<f32> {
    fn distance_from_origin(&self) -> f32 {
        (self.x.powi(2) + self.y.powi(2)).sqrt()
    }
}
```

## Trait Bounds

Type parameters can be constrained using trait bounds, ensuring that the generic type implements specific behavior. While trait bounds can be placed directly on the struct definition, idiomatic Rust typically places them on the `impl` block to avoid redundant bound declarations across the codebase.

**Bound on the `impl` block (Idiomatic):**

```rust theme={"dark"}
impl<T: std::fmt::Display> Point<T> {
    fn print_coordinates(&self) {
        println!("({}, {})", self.x, self.y);
    }
}
```

**Bound on the struct definition (Strict):**

```rust theme={"dark"}
struct BoundedPoint<T: std::fmt::Display> {
    x: T,
    y: T,
}
```

## Unused Type Parameters and `PhantomData`

Rust enforces strict rules regarding generic parameters: every type parameter declared in the struct signature must be consumed by at least one field. If a type parameter is required for logical or type-state purposes but does not represent data stored in memory, the compiler will reject the definition.

To bypass this, you must use `std::marker::PhantomData<T>`. This is a zero-sized type that tells the compiler to act as though the struct owns an instance of `T`, preserving the type parameter without affecting the struct's memory layout.

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

struct StateMachine<T> {
    state_id: u32,
    _marker: PhantomData<T>,
}
```

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