What Is the Difference Between Zero-Sized Types and PhantomData?

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