Why does an enum require extra memory size?

后端 未结 2 1277
一整个雨季
一整个雨季 2020-12-04 01:46

My understanding is that enum is like union in C and the system will allocate the largest of the data types in the enum.

enum E1 {
         


        
2条回答
  •  情话喂你
    2020-12-04 02:10

    In Rust, unlike in C, enums are tagged unions. That is, the enum knows which value it holds. So 8 bytes wouldn't be enough because there would be no room for the tag.

提交回复
热议问题