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>,
}
PhantomData is a zero-sized placeholder that tells the compiler your struct is connected to a specific type or lifetime, even if it doesn't hold any actual data of that type. It matters because it ensures the compiler enforces correct memory safety rules, like preventing you from moving data that shouldn't be moved. Think of it as a label on a box that says "contains fragile items" even if the box is currently empty, ensuring handlers treat it with care.