-
Technical aspect:
[T; 0]
currently requiresT : Sized
, and[T; 0]
has the alignment ofT
(whereasPhantomData
always has an alignment of1
).-
PhantomData<T>
also unconditionally isCopy + Debug + Ord + Hash
A tiny bonus for zero-long arrays is that they require no import or long name, and that instantiating one is very easy:
[]
. -
-
Documentation aspect:
PhantomData
may be a bit better at conveying that the stuff in question is "phantom", whereas an empty array, historically, is more often used for field/struct alignement shenanigans. -
My own rule of thumb:
I personally reach forPhantomData
when inside data types, and for[; 0]
when using it for function arguments, especially user-provided closure arguments when tinkering with higher-order signatures or whatnot (since the non-1
-alignment of a zero-sized parameter should play no role whatsoever in the machine code generated around function calls)
10 Likes