Skip to main content

Module vec

Module vec 

1.36.0 · Source
Expand description

A contiguous growable array type with heap-allocated contents, written Vec<T>.

Vectors have O(1) indexing, amortized O(1) push (to the end) and O(1) pop (from the end).

Vectors ensure they never allocate more than isize::MAX bytes.

§Examples

You can explicitly create a Vec with Vec::new:

let v: Vec<i32> = Vec::new();

…or by using the vec! macro:

let v: Vec<i32> = vec![];

let v = vec![1, 2, 3, 4, 5];

let v = vec![0; 10]; // ten zeroes

You can push values onto the end of a vector (which will grow the vector as needed):

let mut v = vec![1, 2];

v.push(3);

Popping values works in much the same way:

let mut v = vec![1, 2];

let two = v.pop();

Vectors also support indexing (through the Index and IndexMut traits):

let mut v = vec![1, 2, 3];
let three = v[2];
v[1] = v[1] + 5;

§Memory layout

A Vec conceptually consists of a raw pointer ptr to the start of the allocation, a length len, a capacity cap, and an allocator alloc. While the exact fields and their order are not specified, a Vec can be converted to and from these parts using Vec::into_raw_parts_with_allocator, Vec::from_raw_parts_in, Vec::into_raw_parts, and Vec::from_raw_parts, as well as their NonNull-based versions.

The parts of a Vec have the following invariants:

  • If size_of::<T>() and cap are non-zero, then:

    • ptr must point to the start of a memory block currently allocated via alloc.
    • Layout::array::<T>(cap) must fit the memory block of ptr. In particular,
      • T must have the same alignment as the Layout ptr was allocated with, and
      • size_of::<T>() * cap must be at least the size of the Layout ptr was allocated with, and at most the size of the allocation returned the allocator.
    • size_of::<T>() * cap must be at most isize::MAX. See the safety documentation of pointer::offset.
  • If size_of::<T>() or cap is zero, then ptr must be any sufficiently aligned non-null pointer.

    NOTE: ptr will not be passed to the allocator for deallocation by the destructor of Vec in this case. Thus, it is unwise to use a pointer acquired from the allocator for this, as overallocated memory may end up being leaked. Use ptr::dangling instead.

  • The following must always be satisfied:

    • len needs to be less than or equal to cap.
    • At ptr, there are at least len valid values of type T, laid out contiguously (like an array). These values are considered owned by the Vec.

It is sound to create a Vec from parts that satisfy these invariants.

Conversely, the memory backing ptr obtained from Vec::as_mut_ptr or Vec::into_raw_parts_with_allocator-like methods may be deallocated using the allocator, with layout Layout::array::<T>(cap), provided that size_of::<T>() or cap is non-zero. To automatically handle the case where the allocation is zero-sized, it is recommended to use Vec::from_raw_parts_in, instead of manually deallocating.

Structs§

Drain
A draining iterator for Vec<T>.
ExtractIf
An iterator which uses a closure to determine if an element should be removed.
IntoIter
An iterator that moves out of a vector.
Splice
A splicing iterator for Vec.
Vec
A contiguous growable array type, written as Vec<T>, short for ‘vector’.
PeekMutExperimental
Structure wrapping a mutable reference to the last item in a Vec.