pub trait Animal {}

fn test_00() {
    println!("{:?}", std::mem::size_of::<Box<u8>>());
    println!("{:?}", std::mem::size_of::<Box<[u32; 16]>>());
    println!("{:?}", std::mem::size_of::<Box<dyn Animal>>());}

returns 8, 8, 16

I understand the concept of fat pointers. What I don't understand here is how Box changes its size. What's going on here?

If you dig through the newtypes, a Box<T> is basically a *const T or *mut T. Those are wide or not depending on T.



Then why the last one is twice as large?

From here :

As such, a dyn Trait reference contains two pointers. One pointer goes to the data (e.g., an instance of a struct). Another pointer goes to a map of method call names to function pointers (known as a virtual method table or vtable).


This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.