Does Rust include individual elements that are added to the vector?

According to the Rust documentation:

Vectors always allocate their data on the heap.

As I understand it, this means that:

  • Rust allocates enough memory on the heap to keep the type Tcontiguous.
  • Rust will not individually place elements, since they are placed in a vector.

In other words, if I add several integers to the vector, and Vecallocates enough storage to store these integers, it will also not insert these integers; introducing another layer of indirection.

I'm not sure how I can illustrate or confirm this with code examples, but any help would be appreciated.

+4
source share
1 answer

Yes , it Vec<T>will store all elements in an adjacent buffer, and not separately in the box. The documentation states:

The conjugate type of a massive array, written Vec<T>but expressed as a "vector."

Note that you can also cut the vector to get &[T](slice). His documentation confirms this:

Dynamically dimensional view of a continuous sequence [T].

Slices are a representation in a block of memory, represented as a pointer and length.

+6
source

Source: https://habr.com/ru/post/1655708/


All Articles