3

The docs for size_of say the following:

If T is Sized, all of those types [pointer types such as &T] have the same size as usize.

Why that qualifier? Would a pointer not have a definitive size regardless of any property of the type being pointed to?

2

1 Answer 1

5

Unsized object references have extra data in addition to the pointer to the object. In the case of slice references (&[T]), they contain a size in order to indicate how long this slice is. And in the case of references to traits (trait objects), they contain a pointer to a vtable in order to enable dynamic dispatch.

Sign up to request clarification or add additional context in comments.

1 Comment

now that it's written down in front of me, it seems painfully obvious... I should have thought more carefully what kinds of unsized types there are. Thanks!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.