I am confused about why the size of Vec<i64> and VecEnum is the same in the following code:
pub enum VecEnum {
Abc,
Vec(Vec<i64>),
}
pub enum IntEnum {
Abc,
Int(i64),
}
pub fn main() {
println!("IntEnum: {} bytes", core::mem::size_of::<IntEnum>());
println!("i64: {} bytes", core::mem::size_of::<i64>());
println!("VecEnum: {} bytes", core::mem::size_of::<VecEnum>());
println!("Vec<i64>: {} bytes", core::mem::size_of::<Vec<i64>>());
}
This outputs the following:
IntEnum: 16 bytes
i64: 8 bytes
VecEnum: 24 bytes
Vec<i64>: 24 bytes
For the i64 it behaves as expected: having an enum with a i64 variant requires extra space for the enum tag to be encoded. But why is this not the case for the Vec, which just consists of 3 8-byte values (ptr,len,capacity) of stack memory?
Can someone explain how the memory layout works here and what is happening under the hood?