Use PhantomData<T> as a struct field to inform the compiler about ownership or borrowing relationships for types that are not directly stored.
Use PhantomData<T> as a struct field to tell the compiler your type logically owns or borrows T without storing it, ensuring correct variance and drop behavior. Add use std::marker::PhantomData; and define a private field like _marker: PhantomData<T> in your struct.
use std::marker::PhantomData;
struct MyStruct<T> {
_marker: PhantomData<T>,
}
This pattern is essential for types like TypedArena<T> where the compiler needs to know that dropping the arena must drop the T instances it manages, even if T isn't a direct field.
PhantomData is a zero-sized marker that tells the compiler your code interacts with a specific type, even if you don't actually store any data of that type. It ensures the compiler enforces correct rules for memory safety, like when your struct should be dropped or if it can be shared between threads. Think of it as a label on a box that says 'contains apples' so the system handles it correctly, even if the box is currently empty.