Struct ArrayVec
#[repr(C)]
pub struct ArrayVec<T, const CAP: usize> { /* private fields */ }
A vector with a fixed capacity.
The ArrayVec is a vector backed by a fixed size array. It keeps track of
the number of initialized elements. The ArrayVec<T, CAP> is parameterized
by T for the element type and CAP for the maximum capacity.
CAP is of type usize but is range limited to u32::MAX (or u16::MAX on 16-bit targets);
attempting to create larger arrayvecs with larger capacity will panic.
The vector is a contiguous value (storing the elements inline) that you can store directly on the stack if needed.
It offers a simple API but also dereferences to a slice, so that the full slice API is available. The ArrayVec can be converted into a by value iterator.
Implementations
impl<T, const CAP: usize> ArrayVec<T, CAP>
fn new() -> ArrayVec<T, CAP>Create a new empty
ArrayVec.The maximum capacity is given by the generic parameter
CAP.use ArrayVec; let mut array = new; array.push; array.push; assert_eq!; assert_eq!;const fn new_const() -> ArrayVec<T, CAP>Create a new empty
ArrayVec(const fn).The maximum capacity is given by the generic parameter
CAP.use ArrayVec; static ARRAY: = new_const;const fn len(&self) -> usizeReturn the number of elements in the
ArrayVec.use ArrayVec; let mut array = from; array.pop; assert_eq!;const fn is_empty(&self) -> boolReturns whether the
ArrayVecis empty.use ArrayVec; let mut array = from; array.pop; assert_eq!;const fn capacity(&self) -> usizeReturn the capacity of the
ArrayVec.use ArrayVec; let array = from; assert_eq!;const fn is_full(&self) -> boolReturn true if the
ArrayVecis completely filled to its capacity, false otherwise.use ArrayVec; let mut array = new; assert!; array.push; assert!;const fn remaining_capacity(&self) -> usizeReturns the capacity left in the
ArrayVec.use ArrayVec; let mut array = from; array.pop; assert_eq!;fn push(&mut self, element: T)Push
elementto the end of the vector.Panics if the vector is already full.
use ArrayVec; let mut array = new; array.push; array.push; assert_eq!;fn try_push(&mut self, element: T) -> Result<(), CapacityError<T>>Push
elementto the end of the vector.Return
Okif the push succeeds, or return an error if the vector is already full.use ArrayVec; let mut array = new; let push1 = array.try_push; let push2 = array.try_push; assert!; assert!; assert_eq!; let overflow = array.try_push; assert!;unsafe fn push_unchecked(&mut self, element: T)Push
elementto the end of the vector without checking the capacity.It is up to the caller to ensure the capacity of the vector is sufficiently large.
This method uses debug assertions to check that the arrayvec is not full.
use ArrayVec; let mut array = new; if array.len + 2 <= array.capacity assert_eq!;fn truncate(&mut self, new_len: usize)Shortens the vector, keeping the first
lenelements and dropping the rest.If
lenis greater than the vector’s current length this has no effect.use ArrayVec; let mut array = from; array.truncate; assert_eq!; array.truncate; assert_eq!;fn clear(&mut self)Remove all elements in the vector.
fn insert(&mut self, index: usize, element: T)Insert
elementat positionindex.Shift up all elements after
index.It is an error if the index is greater than the length or if the arrayvec is full.
Panics if the array is full or the
indexis out of bounds. Seetry_insertfor fallible version.use ArrayVec; let mut array = new; array.insert; array.insert; assert_eq!;fn try_insert(&mut self, index: usize, element: T) -> Result<(), CapacityError<T>>Insert
elementat positionindex.Shift up all elements after
index; theindexmust be less than or equal to the length.Returns an error if vector is already at full capacity.
Panics
indexis out of bounds.use ArrayVec; let mut array = new; assert!; assert!; assert!; assert_eq!;fn pop(&mut self) -> Option<T>Remove the last element in the vector and return it.
Return
Some(element)if the vector is non-empty, elseNone.use ArrayVec; let mut array = new; array.push; assert_eq!; assert_eq!;fn swap_remove(&mut self, index: usize) -> TRemove the element at
indexand swap the last element into its place.This operation is O(1).
Return the element if the index is in bounds, else panic.
Panics if the
indexis out of bounds.use ArrayVec; let mut array = from; assert_eq!; assert_eq!; assert_eq!; assert_eq!;fn swap_pop(&mut self, index: usize) -> Option<T>Remove the element at
indexand swap the last element into its place.This is a checked version of
.swap_remove.
This operation is O(1).Return
Some(element)if the index is in bounds, elseNone.use ArrayVec; let mut array = from; assert_eq!; assert_eq!; assert_eq!;fn remove(&mut self, index: usize) -> TRemove the element at
indexand shift down the following elements.The
indexmust be strictly less than the length of the vector.Panics if the
indexis out of bounds.use ArrayVec; let mut array = from; let removed_elt = array.remove; assert_eq!; assert_eq!;fn pop_at(&mut self, index: usize) -> Option<T>Remove the element at
indexand shift down the following elements.This is a checked version of
.remove(index). ReturnsNoneif there is no element atindex. Otherwise, return the element insideSome.use ArrayVec; let mut array = from; assert!; assert_eq!; assert!; assert!;fn retain<F>(&mut self, f: F) where F: FnMut(&mut T) -> bool,Retains only the elements specified by the predicate.
In other words, remove all elements
esuch thatf(&mut e)returns false. This method operates in place and preserves the order of the retained elements.use ArrayVec; let mut array = from; array.retain; assert_eq!;fn spare_capacity_mut(&mut self) -> &mut [MaybeUninit<T>]Returns the remaining spare capacity of the vector as a slice of
MaybeUninit<T>.The returned slice can be used to fill the vector with data (e.g. by reading from a file) before marking the data as initialized using the
set_lenmethod.Examples
use ArrayVec; // Allocate vector big enough for 10 elements. let mut v: = new; // Fill in the first 3 elements. let uninit = v.spare_capacity_mut; uninit.write; uninit.write; uninit.write; // Mark the first 3 elements of the vector as being initialized. unsafe assert_eq!;unsafe fn set_len(&mut self, length: usize)Set the vector’s length without dropping or moving out elements
This method is
unsafebecause it changes the notion of the number of “valid” elements in the vector. Use with care.This method uses debug assertions to check that
lengthis not greater than the capacity.fn try_extend_from_slice(&mut self, other: &[T]) -> Result<(), CapacityError> where T: Copy,Copy all elements from the slice and append to the
ArrayVec.use ArrayVec; let mut vec: = new; vec.push; vec.try_extend_from_slice.unwrap; assert_eq!;Errors
This method will return an error if the capacity left (see
remaining_capacity) is smaller then the length of the provided slice.fn drain<R>(&mut self, range: R) -> Drain<'_, T, CAP> where R: RangeBounds<usize>,Create a draining iterator that removes the specified range in the vector and yields the removed items from start to end. The element range is removed even if the iterator is not consumed until the end.
Note: It is unspecified how many elements are removed from the vector, if the
Drainvalue is leaked.Panics if the starting point is greater than the end point or if the end point is greater than the length of the vector.
use ArrayVec; let mut v1 = from; let v2: = v1.drain.collect; assert_eq!; assert_eq!;fn into_inner(self) -> Result<[T; CAP], Self>Return the inner fixed size array, if it is full to its capacity.
Return an
Okvalue with the array if length equals capacity, return anErrwith self otherwise.unsafe fn into_inner_unchecked(self) -> [T; CAP]Return the inner fixed size array.
Safety: This operation is safe if and only if length equals capacity.
fn take(&mut self) -> SelfReturns the ArrayVec, replacing the original with a new empty ArrayVec.
use ArrayVec; let mut v = from; assert_eq!; assert!;fn as_slice(&self) -> &[T]Return a slice containing all elements of the vector.
fn as_mut_slice(&mut self) -> &mut [T]Return a mutable slice containing all elements of the vector.
fn as_ptr(&self) -> *const TReturn a raw pointer to the vector's buffer.
fn as_mut_ptr(&mut self) -> *mut TReturn a raw mutable pointer to the vector's buffer.
Trait Implementations
impl<T, const CAP: usize> AsMut<[T]> for ArrayVec<T, CAP>
fn as_mut(&mut self) -> &mut [T]
impl<T, const CAP: usize> AsRef<[T]> for ArrayVec<T, CAP>
fn as_ref(&self) -> &[T]
impl<T, const CAP: usize> Borrow<[T]> for ArrayVec<T, CAP>
fn borrow(&self) -> &[T]
impl<T, const CAP: usize> BorrowMut<[T]> for ArrayVec<T, CAP>
fn borrow_mut(&mut self) -> &mut [T]
impl<T, const CAP: usize> Clone for ArrayVec<T, CAP>
where
T: Clone,
fn clone(&self) -> Selffn clone_from(&mut self, rhs: &Self)
impl<T, const CAP: usize> Debug for ArrayVec<T, CAP>
where
T: Debug,
fn fmt(&self, f: &mut Formatter<'_>) -> Result
impl<T, const CAP: usize> Default for ArrayVec<T, CAP>
fn default() -> ArrayVec<T, CAP>Return an empty array
impl<T, const CAP: usize> Deref for ArrayVec<T, CAP>
type Target = [T];fn deref(&self) -> &Self::Target
impl<T, const CAP: usize> DerefMut for ArrayVec<T, CAP>
fn deref_mut(&mut self) -> &mut Self::Target
impl<T, const CAP: usize> Drop for ArrayVec<T, CAP>
fn drop(&mut self)
impl<T, const CAP: usize> Eq for ArrayVec<T, CAP>
where
T: Eq,
impl<T, const CAP: usize> Extend<T> for ArrayVec<T, CAP>
fn extend<I: IntoIterator<Item = T>>(&mut self, iter: I)Extend the
ArrayVecwith an iterator.Panics if extending the vector exceeds its capacity.
impl<T, const CAP: usize> From<[T; CAP]> for ArrayVec<T, CAP>
fn from(array: [T; CAP]) -> Self
impl<T, const CAP: usize> FromIterator<T> for ArrayVec<T, CAP>
fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> SelfCreate an
ArrayVecfrom an iterator.Panics if the number of elements in the iterator exceeds the arrayvec's capacity.
impl<T, const CAP: usize> Hash for ArrayVec<T, CAP>
where
T: Hash,
fn hash<H: Hasher>(&self, state: &mut H)
impl<T, const CAP: usize> IntoIterator for ArrayVec<T, CAP>
type Item = T;type IntoIter = IntoIter<T, CAP>;fn into_iter(self) -> IntoIter<T, CAP>
impl<T, const CAP: usize> Ord for ArrayVec<T, CAP>
where
T: Ord,
fn cmp(&self, other: &Self) -> Ordering
impl<T, const CAP: usize> PartialEq for ArrayVec<T, CAP>
where
T: PartialEq,
fn eq(&self, other: &Self) -> bool
impl<T, const CAP: usize> PartialEq<[T]> for ArrayVec<T, CAP>
where
T: PartialEq,
fn eq(&self, other: &[T]) -> bool
impl<T, const CAP: usize> PartialOrd for ArrayVec<T, CAP>
where
T: PartialOrd,
fn partial_cmp(&self, other: &Self) -> Option<Ordering>fn lt(&self, other: &Self) -> boolfn le(&self, other: &Self) -> boolfn ge(&self, other: &Self) -> boolfn gt(&self, other: &Self) -> bool
impl<T, const CAP: usize> TryFrom<&[T]> for ArrayVec<T, CAP>
where
T: Clone,
type Error = CapacityError;fn try_from(slice: &[T]) -> Result<Self, Self::Error>
impl<const CAP: usize> Write for ArrayVec<u8, CAP>
fn write(&mut self, data: &[u8]) -> Result<usize>fn flush(&mut self) -> Result<()>
Auto Trait Implementations
impl<T, const CAP: usize> Freeze for ArrayVec<T, CAP>
where
[MaybeUninit<T>; CAP]: Freeze,
impl<T, const CAP: usize> RefUnwindSafe for ArrayVec<T, CAP>
where
[MaybeUninit<T>; CAP]: RefUnwindSafe,
impl<T, const CAP: usize> Send for ArrayVec<T, CAP>
where
[MaybeUninit<T>; CAP]: Send,
impl<T, const CAP: usize> Sync for ArrayVec<T, CAP>
where
[MaybeUninit<T>; CAP]: Sync,
impl<T, const CAP: usize> Unpin for ArrayVec<T, CAP>
where
[MaybeUninit<T>; CAP]: Unpin,
impl<T, const CAP: usize> UnsafeUnpin for ArrayVec<T, CAP>
where
[MaybeUninit<T>; CAP]: UnsafeUnpin,
impl<T, const CAP: usize> UnwindSafe for ArrayVec<T, CAP>
where
[MaybeUninit<T>; CAP]: UnwindSafe,
Blanket Implementations
impl<P, T> Receiver for ArrayVec<T, CAP>
where
P: Deref<Target = T> + ?Sized,
T: ?Sized,
type Target = T;
impl<T> Any for ArrayVec<T, CAP>
where
T: 'static + ?Sized,
fn type_id(&self) -> TypeId
impl<T> Borrow<T> for ArrayVec<T, CAP>
where
T: ?Sized,
fn borrow(&self) -> &T
impl<T> BorrowMut<T> for ArrayVec<T, CAP>
where
T: ?Sized,
fn borrow_mut(&mut self) -> &mut T
impl<T> CloneToUninit for ArrayVec<T, CAP>
where
T: Clone,
unsafe fn clone_to_uninit(&self, dest: *mut u8)
impl<T> From<T> for ArrayVec<T, CAP>
fn from(t: T) -> TReturns the argument unchanged.
impl<T> ToOwned for ArrayVec<T, CAP>
where
T: Clone,
type Owned = T;fn to_owned(&self) -> Tfn clone_into(&self, target: &mut T)
impl<T, U> Into<U> for ArrayVec<T, CAP>
where
U: From<T>,
fn into(self) -> UCalls
U::from(self).That is, this conversion is whatever the implementation of
[From]<T> for Uchooses to do.
impl<T, U> TryFrom<U> for ArrayVec<T, CAP>
where
U: Into<T>,
type Error = never;fn try_from(value: U) -> Result<T, never>
impl<T, U> TryInto<U> for ArrayVec<T, CAP>
where
U: TryFrom<T>,
type Error = <U as TryFrom<T>>::Error;fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>