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:
…or by using the vec! macro:
You can push values onto the end of a vector (which will grow the vector
as needed):
Popping values works in much the same way:
Vectors also support indexing (through the Index and IndexMut traits):
§Memory layout
When the type is non-zero-sized and the capacity is nonzero, Vec uses the Global
allocator for its allocation. It is valid to convert both ways between such a Vec and a raw
pointer allocated with the Global allocator, provided that the Layout used with the
allocator is correct for a sequence of capacity elements of the type, and the first len
values pointed to by the raw pointer are valid. More precisely, a ptr: *mut T that has been
allocated with the Global allocator with Layout::array::<T>(capacity) may
be converted into a vec using
Vec::<T>::from_raw_parts(ptr, len, capacity). Conversely, the memory
backing a value: *mut T obtained from Vec::<T>::as_mut_ptr may be deallocated using the
Global allocator with the same layout.
For zero-sized types (ZSTs), or when the capacity is zero, the Vec pointer must be non-null
and sufficiently aligned. The recommended way to build a Vec of ZSTs if vec! cannot be
used is to use ptr::NonNull::dangling.
Re-exports§
pub use self::extract_if::ExtractIf;pub use self::splice::Splice;pub use self::drain::Drain;pub use self::into_iter::IntoIter;pub use self::peek_mut::PeekMut;Experimental
Modules§
- cow 🔒
- drain 🔒
- extract_
if 🔒 - in_
place_ 🔒collect - Inplace iterate-and-collect specialization for
Vec - in_
place_ 🔒drop - into_
iter 🔒 - is_zero 🔒
- partial_
eq 🔒 - peek_
mut 🔒 - set_
len_ 🔒on_ drop - spec_
extend 🔒 - spec_
from_ 🔒elem - spec_
from_ 🔒iter - spec_
from_ 🔒iter_ nested - splice 🔒
Structs§
- Vec
- A contiguous growable array type, written as
Vec<T>, short for ‘vector’.
Traits§
- Extend
From 🔒Within Spec - Recyclable 🔒
- Denotes that an allocation of
Fromcan be recycled into an allocation ofSelf.
Functions§
- from_
elem 👻 - from_
elem_ 👻in Experimental