Expand description
A contiguous growable array type with heap-allocated contents, writtenVec<T>.
Vectors haveO(1) indexing, amortizedO(1) push (to the end) andO(1) pop (from the end).
Vectors ensure they never allocate more thanisize::MAX bytes.
§Examples
You can explicitly create aVec withVec::new:
…or by using thevec! macro:
You canpush values onto the end of a vector (which will grow the vectoras needed):
Popping values works in much the same way:
Vectors also support indexing (through theIndex andIndexMut traits):
§Memory layout
When the type is non-zero-sized and the capacity is nonzero,Vec uses theGlobalallocator for its allocation. It is valid to convert both ways between such aVec and a rawpointer allocated with theGlobal allocator, provided that theLayout used with theallocator is correct for a sequence ofcapacity elements of the type, and the firstlenvalues pointed to by the raw pointer are valid. More precisely, aptr: *mut T that has beenallocated with theGlobal allocator withLayout::array::<T>(capacity) maybe converted into a vec usingVec::<T>::from_raw_parts(ptr, len, capacity). Conversely, the memorybacking avalue: *mut T obtained fromVec::<T>::as_mut_ptr may be deallocated using theGlobal allocator with the same layout.
For zero-sized types (ZSTs), or when the capacity is zero, theVec pointer must be non-nulland sufficiently aligned. The recommended way to build aVec of ZSTs ifvec! cannot beused is to useptr::NonNull::dangling.
Structs§
- Drain
- A draining iterator for
Vec<T>. - Extract
If - An iterator which uses a closure to determine if an element should be removed.
- Into
Iter - 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’. - PeekMut
Experimental - Structure wrapping a mutable reference to the last item in a
Vec.