In Learning Rust With Entirely Too Many Linked Lists, the author mentions:
However, if we have a special kind of enum:
enum Foo { A, B(ContainsANonNullPtr), }
the null pointer optimization kicks in, which eliminates the space needed for the tag. If the variant is
A
, the whole enum is set to all0
's. Otherwise, the variant isB
. This works becauseB
can never be all0
's, since it contains a non-zero pointer.
I guess that the author is saying that (assuming A
is 4 bits, and B
is 4 bits)
let test = Foo::A
the memory layout is
0000 0000
but
let test = Foo::B
the memory layout is
some 8 bit non 0 value
What exactly is optimized here? Aren't both representation always 8 bits What does it mean when the author claims
It means
&
,&mut
,Box
,Rc
,Arc
,Vec
, and several other important types in Rust have no overhead when put in anOption