问题描述
根据Rust文档:
据我了解,这意味着:
- Rust将在堆上分配足够的内存以连续存储类型
T
. - 铁锈不会将它们放入矢量中时单独装箱.
Vec
将分配足够的存储空间来存储这些整数,那么它也不会将这些整数装箱.引入另一层间接.我不确定如何用代码示例来说明或确认这一点,但会有所帮助.
是,Vec<T>
会将所有项目存储在连续的缓冲区中,而不是单独装箱. 文档指出:
请注意,也可以对向量进行切片,以获得&[T]
(切片).再次其文档确认这一点:
According to the Rust documentation:
As I understand this, it means that:
- Rust will allocate enough memory on the heap to store the type
T
in a contiguous fashion. - Rust will not individually box the items as they are placed into the vector.
In other words, if I add a few integers to a vector, while the Vec
will allocate enough storage to store those integers, it's not also going to box those integers; introducing another layer of indirection.
I'm not sure how I can illustrate or confirm this with code examples but any help is appreciated.
Yes, Vec<T>
will store all items in a contiguous buffer rather than boxing them individually. The documentation states:
Note that it is also possible to slice a vector, to get a &[T]
(slice). Its documentation, again, confirms this:
这篇关于Rust是否会将添加到矢量中的单个项目装箱?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!