Conceptual
Login

Marking Unused Type Parameters with PhantomData

Sometimes a type needs to be generic over something it does not actually store — a kind of device, or a lifetime it is only borrowing conceptually. PhantomData is a zero-size field that tells the compiler "pretend this type is in here" so the usual ownership and lifetime checks still apply. It costs nothing at run time and exists purely to keep the type system honest.

Questions this Concept answers

  • Why would a type be generic over a parameter it never actually stores?