Why does Rust not allow coercion to trait objects inside containers?

后端 未结 1 1198
抹茶落季
抹茶落季 2021-01-18 01:34

I have a Vec> where T implements Foo. Why can I not coerce it to a Vec> even thou

相关标签:
1条回答
  • 2021-01-18 01:47

    Because Box<Bar> is a different size than Box<Foo>. The coercion is allowed on a single value, but here you'd have to resize the whole vector. The book goes into some detail on this in the section on Representation of Trait Objects. Short version: Box<Bar> is a pointer to a value. Box<Foo> is a pointer to a value and a pointer to a vtable.

    0 讨论(0)
提交回复
热议问题