What is the null pointer optimization in Rust?

后端 未结 2 2040
悲哀的现实
悲哀的现实 2020-12-06 04:28

In Learning Rust With Entirely Too Many Linked Lists, the author mentions:

However, if we have a special kind of enum:

enum Foo {
             


        
2条回答
  •  醉梦人生
    2020-12-06 05:20

    enum is a tagged union. Without optimization it looks like

    Foo::A;    // tag 0x00 data 0xXX
    Foo::B(2); // tag 0x01 data 0x02
    

    The null pointer optimization removes the separate tag field.

    Foo::A;    // tag+data 0x00
    Foo::B(2); // tag+data 0x02
    

提交回复
热议问题