How to Use PhantomData in Struct Definitions

Use PhantomData<T> in struct definitions to inform the compiler about ownership or lifetime relationships without storing actual data.

Use PhantomData<T> as a struct field to tell the compiler that your type logically owns or borrows data of type T without actually storing it. This is required to satisfy variance rules, implement traits like Send or Sync correctly, or enforce lifetime constraints on generic parameters.

use std::marker::PhantomData;

struct MyStruct<'a, T> {
    _marker: PhantomData<&'a T>,
}