Why does Nil increase one enum size but not another? How is memory allocated for Rust enums?
- 阅读更多 关于 Why does Nil increase one enum size but not another? How is memory allocated for Rust enums?
问题 If I define the following enums, Nil does not increase the size of the enum: use std::mem::size_of; enum Foo { Cons(~char) } enum Bar { Cons(~char), Nil } println!("{}", size_of::<Foo>()); println!("{}", size_of::<Bar>()); // -> 4 // -> 4 On the other hand: enum Foo { Cons(char) } enum Foo { Cons(char), Nil } Yields: // -> 4 // -> 8 What is happening when I define an enum? How is memory being allocated for these structures? 回答1: A naive approach to enums is to allocate enough space for the