Simple answer about traits

What is difference between Unsize and CoerceUnsized traits?

2 Likes

Unsize is a marker that’s automatically provided by the compiler because it knows how to create a fat pointer to a sized type.

CoerceUnsized means you’re a (smart usually) pointer to a type that can be unsized (ie the type it’s pointing at is Unsize). This is what allows unsizing coercions like Box<i32> as Box<std::fmt::Display> or Box<[u8;2]> as Box<[u8]>. The coercion essentially causes the compiler to form the fat ptr.

Not sure if this is a good enough explanation though ...

1 Like

Thank you