Zero-sized types take no memory, while PhantomData is a zero-sized type used to enforce ownership and variance rules for types not actually stored in a struct.
Zero-sized types (ZSTs) occupy no memory and are used for compile-time markers, while PhantomData<T> is a ZST that explicitly tells the compiler a type logically owns or references T to enforce correct variance and drop behavior. Use PhantomData when your struct needs to satisfy trait bounds or ownership rules for a type it doesn't actually store.
use std::marker::PhantomData;
struct Marker {
// No fields, size is 0
}
struct Wrapper<T> {
// Holds no data, but acts as if it owns T
_marker: PhantomData<T>,
}
A zero-sized type is a placeholder that takes up no space in memory, often used just to signal a concept to the compiler. PhantomData is a specific zero-sized type that tells the compiler, "I pretend to own this other type," which ensures your code follows safety rules like dropping data correctly. Think of it like a label on an empty box that tells the warehouse system what kind of item used to be inside, so it handles the box correctly even though the box is empty.