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

后端 未结 1 1201
抹茶落季
抹茶落季 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 is a different size than Box. 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 is a pointer to a value. Box is a pointer to a value and a pointer to a vtable.

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