# [−][src]Struct arrayvec::ArrayVec

    pub struct ArrayVec<A: Array> { /* fields omitted */ }

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 vector is a contiguous value 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.

ArrayVec can be converted into a by value iterator.

## Methods

### `impl<A: Array> ArrayVec<A>`[src]

#### `pub fn new() -> ArrayVec<A>`[src]

Create a new empty `ArrayVec`.

Capacity is inferred from the type parameter.

    use arrayvec::ArrayVec;

    let mut array = ArrayVec::<[_; 16]>::new();
    array.push(1);
    array.push(2);
    assert_eq!(&array[..], &[1, 2]);
    assert_eq!(array.capacity(), 16);

#### `pub fn len(&self) -> usize`[src]

Return the number of elements in the `ArrayVec`.

    use arrayvec::ArrayVec;

    let mut array = ArrayVec::from([1, 2, 3]);
    array.pop();
    assert_eq!(array.len(), 2);

#### `pub fn capacity(&self) -> usize`[src]

Return the capacity of the `ArrayVec`.

    use arrayvec::ArrayVec;

    let array = ArrayVec::from([1, 2, 3]);
    assert_eq!(array.capacity(), 3);

#### `pub fn is_full(&self) -> bool`[src]

Return if the `ArrayVec` is completely filled.

    use arrayvec::ArrayVec;

    let mut array = ArrayVec::<[_; 1]>::new();
    assert!(!array.is_full());
    array.push(1);
    assert!(array.is_full());

#### `pub fn push(&mut self, element: A::Item)`[src]

Push `element` to the end of the vector.

Panics if the vector is already full.

    use arrayvec::ArrayVec;

    let mut array = ArrayVec::<[_; 2]>::new();

    array.push(1);
    array.push(2);

    assert_eq!(&array[..], &[1, 2]);

#### `pub fn try_push(
&mut self,
element: A::Item
) -> Result<(), CapacityError<A::Item>>`[src]

Push `element` to the end of the vector.

Return `Ok` if the push succeeds, or return an error if the vector is already full.

    use arrayvec::ArrayVec;

    let mut array = ArrayVec::<[_; 2]>::new();

    let push1 = array.try_push(1);
    let push2 = array.try_push(2);

    assert!(push1.is_ok());
    assert!(push2.is_ok());

    assert_eq!(&array[..], &[1, 2]);

    let overflow = array.try_push(3);

    assert!(overflow.is_err());

#### `pub unsafe fn push_unchecked(&mut self, element: A::Item)`[src]

Push `element` to 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::ArrayVec;

    let mut array = ArrayVec::<[_; 2]>::new();

    if array.len() + 2 <= array.capacity() {
        unsafe {
            array.push_unchecked(1);
            array.push_unchecked(2);
        }
    }

    assert_eq!(&array[..], &[1, 2]);

#### `pub fn insert(&mut self, index: usize, element: A::Item)`[src]

Insert `element` at position `index`.

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 `index` is out of bounds. See `try_insert` for fallible version.

    use arrayvec::ArrayVec;

    let mut array = ArrayVec::<[_; 2]>::new();

    array.insert(0, "x");
    array.insert(0, "y");
    assert_eq!(&array[..], &["y", "x"]);

#### `pub fn try_insert(
&mut self,
index: usize,
element: A::Item
) -> Result<(), CapacityError<A::Item>>`[src]

Insert `element` at position `index`.

Shift up all elements after `index`; the `index` must be less than or equal to the length.

Returns an error if vector is already at full capacity.

Panics `index` is out of bounds.

    use arrayvec::ArrayVec;

    let mut array = ArrayVec::<[_; 2]>::new();

    assert!(array.try_insert(0, "x").is_ok());
    assert!(array.try_insert(0, "y").is_ok());
    assert!(array.try_insert(0, "z").is_err());
    assert_eq!(&array[..], &["y", "x"]);

#### `pub fn pop(&mut self) -> Option<A::Item>`[src]

Remove the last element in the vector and return it.

Return `Some(` element `)` if the vector is non-empty, else `None`.

    use arrayvec::ArrayVec;

    let mut array = ArrayVec::<[_; 2]>::new();

    array.push(1);

    assert_eq!(array.pop(), Some(1));
    assert_eq!(array.pop(), None);

#### `pub fn swap_remove(&mut self, index: usize) -> A::Item`[src]

Remove the element at `index` and 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 `index` is out of bounds.

    use arrayvec::ArrayVec;

    let mut array = ArrayVec::from([1, 2, 3]);

    assert_eq!(array.swap_remove(0), 1);
    assert_eq!(&array[..], &[3, 2]);

    assert_eq!(array.swap_remove(1), 2);
    assert_eq!(&array[..], &[3]);

#### `pub fn swap_pop(&mut self, index: usize) -> Option<A::Item>`[src]

Remove the element at `index` and 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, else `None`.

    use arrayvec::ArrayVec;

    let mut array = ArrayVec::from([1, 2, 3]);

    assert_eq!(array.swap_pop(0), Some(1));
    assert_eq!(&array[..], &[3, 2]);

    assert_eq!(array.swap_pop(10), None);

#### `pub fn remove(&mut self, index: usize) -> A::Item`[src]

Remove the element at `index` and shift down the following elements.

The `index` must be strictly less than the length of the vector.

Panics if the `index` is out of bounds.

    use arrayvec::ArrayVec;

    let mut array = ArrayVec::from([1, 2, 3]);

    let removed_elt = array.remove(0);
    assert_eq!(removed_elt, 1);
    assert_eq!(&array[..], &[2, 3]);

#### `pub fn pop_at(&mut self, index: usize) -> Option<A::Item>`[src]

Remove the element at `index` and shift down the following elements.

This is a checked version of `.remove(index)`. Returns `None` if there is no element at `index`. Otherwise, return the element inside `Some`.

    use arrayvec::ArrayVec;

    let mut array = ArrayVec::from([1, 2, 3]);

    assert!(array.pop_at(0).is_some());
    assert_eq!(&array[..], &[2, 3]);

    assert!(array.pop_at(2).is_none());
    assert!(array.pop_at(10).is_none());

#### `pub fn truncate(&mut self, len: usize)`[src]

Shortens the vector, keeping the first `len` elements and dropping the rest.

If `len` is greater than the vector’s current length this has no effect.

    use arrayvec::ArrayVec;

    let mut array = ArrayVec::from([1, 2, 3, 4, 5]);
    array.truncate(3);
    assert_eq!(&array[..], &[1, 2, 3]);
    array.truncate(4);
    assert_eq!(&array[..], &[1, 2, 3]);

#### `pub fn clear(&mut self)`[src]

Remove all elements in the vector.

#### `pub fn retain<F>(&mut self, f: F) where
F: FnMut(&mut A::Item) -> bool,` [src]

Retains only the elements specified by the predicate.

In other words, remove all elements `e` such that `f(&mut e)` returns false. This method operates in place and preserves the order of the retained elements.

    use arrayvec::ArrayVec;

    let mut array = ArrayVec::from([1, 2, 3, 4]);
    array.retain(|x| *x & 1 != 0 );
    assert_eq!(&array[..], &[1, 3]);

#### `pub unsafe fn set_len(&mut self, length: usize)`[src]

Set the vector’s length without dropping or moving out elements

This method is `unsafe` because it changes the notion of the number of “valid” elements in the vector. Use with care.

This method uses debug assertions to check that check that `length` is not greater than the capacity.

####
ⓘImportant traits for Drain<'a, A>

### Important traits for Drain<'a, A>

`impl<'a, A: Array> Iterator for Drain<'a, A> where
A::Item: 'a, type Item = A::Item;`
`pub fn drain<R: RangeArgument>(&mut self, range: R) -> Drain<A>`[src]

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 `Drain` value 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::ArrayVec;

    let mut v = ArrayVec::from([1, 2, 3]);
    let u: ArrayVec<[_; 3]> = v.drain(0..2).collect();
    assert_eq!(&v[..], &[3]);
    assert_eq!(&u[..], &[1, 2]);

#### `pub fn into_inner(self) -> Result<A, Self>`[src]

Return the inner fixed size array, if it is full to its capacity.

Return an `Ok` value with the array if length equals capacity, return an `Err` with self otherwise.

`Note:` This function may incur unproportionally large overhead to move the array out, its performance is not optimal.

#### `pub fn dispose(self)`[src]

Dispose of `self` without the overwriting that is needed in Drop.

#### `pub fn as_slice(&self) -> &[A::Item]`[src]

Return a slice containing all elements of the vector.

#### `pub fn as_mut_slice(&mut self) -> &mut [A::Item]`[src]

Return a mutable slice containing all elements of the vector.

## Trait Implementations

### `impl<A: Array> DerefMut for ArrayVec<A>`[src]

#### `fn deref_mut(&mut self) -> &mut [A::Item]`[src]

Mutably dereferences the value.

### `impl<A: Array> From<A> for ArrayVec<A>`[src]

Create an `ArrayVec` from an array.

    use arrayvec::ArrayVec;

    let mut array = ArrayVec::from([1, 2, 3]);
    assert_eq!(array.len(), 3);
    assert_eq!(array.capacity(), 3);

#### `fn from(array: A) -> Self`[src]

Performs the conversion.

### `impl<A: Array> PartialEq<ArrayVec<A>> for ArrayVec<A> where
A::Item: PartialEq,` [src]

#### `fn eq(&self, other: &Self) -> bool`[src]

This method tests for `self` and `other` values to be equal, and is used by `==`. Read more

#### `#[must_use] fn ne(&self, other: &Rhs) -> bool` 1.0.0[src]

This method tests for `!=`.

### `impl<A: Array> PartialEq<[<A as Array>::Item]> for ArrayVec<A> where
A::Item: PartialEq,` [src]

#### `fn eq(&self, other: &[A::Item]) -> bool`[src]

This method tests for `self` and `other` values to be equal, and is used by `==`. Read more

#### `#[must_use] fn ne(&self, other: &Rhs) -> bool` 1.0.0[src]

This method tests for `!=`.

### `impl<A: Array> Eq for ArrayVec<A> where
A::Item: Eq,` [src]

### `impl<A: Array> Ord for ArrayVec<A> where
A::Item: Ord,` [src]

#### `fn cmp(&self, other: &ArrayVec<A>) -> Ordering`[src]

This method returns an `Ordering` between `self` and `other`. Read more

#### `fn max(self, other: Self) -> Self` 1.21.0[src]

Compares and returns the maximum of two values. Read more

#### `fn min(self, other: Self) -> Self` 1.21.0[src]

Compares and returns the minimum of two values. Read more

#### `fn clamp(self, min: Self, max: Self) -> Self`[src]

🔬 This is a nightly-only experimental API. (`clamp`)

Restrict a value to a certain interval. Read more

### `impl<A: Array> PartialOrd<ArrayVec<A>> for ArrayVec<A> where
A::Item: PartialOrd,` [src]

#### `fn partial_cmp(&self, other: &ArrayVec<A>) -> Option<Ordering>`[src]

This method returns an ordering between `self` and `other` values if one exists. Read more

#### `fn lt(&self, other: &Self) -> bool`[src]

This method tests less than (for `self` and `other`) and is used by the `<` operator. Read more

#### `fn le(&self, other: &Self) -> bool`[src]

This method tests less than or equal to (for `self` and `other`) and is used by the `<=` operator. Read more

#### `fn ge(&self, other: &Self) -> bool`[src]

This method tests greater than or equal to (for `self` and `other`) and is used by the `>=` operator. Read more

#### `fn gt(&self, other: &Self) -> bool`[src]

This method tests greater than (for `self` and `other`) and is used by the `>` operator. Read more

### `impl<A: Array> Hash for ArrayVec<A> where
A::Item: Hash,` [src]

#### `fn hash<H: Hasher>(&self, state: &mut H)`[src]

Feeds this value into the given [`Hasher`]. Read more

#### `fn hash_slice<H>(data: &[Self], state: &mut H) where
H: Hasher,` 1.3.0[src]

Feeds a slice of this type into the given [`Hasher`]. Read more

### `impl<A: Array> Deref for ArrayVec<A>`[src]

#### `type Target = [A::Item]`

The resulting type after dereferencing.

#### `fn deref(&self) -> &[A::Item]`[src]

Dereferences the value.

### `impl<A: Array> Drop for ArrayVec<A>`[src]

#### `fn drop(&mut self)`[src]

Executes the destructor for this type. Read more

### `impl<A: Array> Debug for ArrayVec<A> where
A::Item: Debug,` [src]

#### `fn fmt(&self, f: &mut Formatter) -> Result`[src]

Formats the value using the given formatter. Read more

### `impl<A: Array> AsRef<[<A as Array>::Item]> for ArrayVec<A>`[src]

#### `fn as_ref(&self) -> &[A::Item]`[src]

Performs the conversion.

### `impl<A: Array> AsMut<[<A as Array>::Item]> for ArrayVec<A>`[src]

#### `fn as_mut(&mut self) -> &mut [A::Item]`[src]

Performs the conversion.

### `impl<A: Array> FromIterator<<A as Array>::Item> for ArrayVec<A>`[src]

Create an `ArrayVec` from an iterator.

Does not extract more items than there is space for. No error occurs if there are more iterator elements.

#### `fn from_iter<T: IntoIterator<Item = A::Item>>(iter: T) -> Self`[src]

Creates a value from an iterator. Read more

### `impl<'a, A: Array> IntoIterator for &'a ArrayVec<A>`[src]

Iterate the `ArrayVec` with references to each element.

    use arrayvec::ArrayVec;

    let array = ArrayVec::from([1, 2, 3]);

    for elt in &array {
        // ...
    }

#### `type Item = &'a A::Item`

The type of the elements being iterated over.

#### `type IntoIter = Iter<'a, A::Item>`

Which kind of iterator are we turning this into?

#### `fn into_iter(self) -> Self::IntoIter`[src]

Creates an iterator from a value. Read more

### `impl<'a, A: Array> IntoIterator for &'a mut ArrayVec<A>`[src]

Iterate the `ArrayVec` with mutable references to each element.

    use arrayvec::ArrayVec;

    let mut array = ArrayVec::from([1, 2, 3]);

    for elt in &mut array {
        // ...
    }

#### `type Item = &'a mut A::Item`

The type of the elements being iterated over.

#### `type IntoIter = IterMut<'a, A::Item>`

Which kind of iterator are we turning this into?

#### `fn into_iter(self) -> Self::IntoIter`[src]

Creates an iterator from a value. Read more

### `impl<A: Array> IntoIterator for ArrayVec<A>`[src]

Iterate the `ArrayVec` with each element by value.

The vector is consumed by this operation.

    use arrayvec::ArrayVec;

    for elt in ArrayVec::from([1, 2, 3]) {
        // ...
    }

#### `type Item = A::Item`

The type of the elements being iterated over.

#### `type IntoIter = IntoIter<A>`

Which kind of iterator are we turning this into?

####
ⓘImportant traits for IntoIter<A>

### Important traits for IntoIter<A>

`impl<A: Array> Iterator for IntoIter<A> type Item = A::Item;`
`fn into_iter(self) -> IntoIter<A>`[src]

Creates an iterator from a value. Read more

### `impl<A: Array> Extend<<A as Array>::Item> for ArrayVec<A>`[src]

Extend the `ArrayVec` with an iterator.

Does not extract more items than there is space for. No error occurs if there are more iterator elements.

#### `fn extend<T: IntoIterator<Item = A::Item>>(&mut self, iter: T)`[src]

Extends a collection with the contents of an iterator. Read more

### `impl<A: Array> Clone for ArrayVec<A> where
A::Item: Clone,` [src]

#### `fn clone(&self) -> Self`[src]

Returns a copy of the value. Read more

#### `fn clone_from(&mut self, rhs: &Self)`[src]

Performs copy-assignment from `source`. Read more

### `impl<A: Array> Default for ArrayVec<A>`[src]

#### `fn default() -> ArrayVec<A>`[src]

Return an empty array

### `impl<A: Array> Borrow<[<A as Array>::Item]> for ArrayVec<A>`[src]

#### `fn borrow(&self) -> &[A::Item]`[src]

Immutably borrows from an owned value. Read more

### `impl<A: Array> BorrowMut<[<A as Array>::Item]> for ArrayVec<A>`[src]

#### `fn borrow_mut(&mut self) -> &mut [A::Item]`[src]

Mutably borrows from an owned value. Read more

## Auto Trait Implementations

### `impl<A> Unpin for ArrayVec<A> where
A: Unpin,
<A as Array>::Index: Unpin,`

### `impl<A> Send for ArrayVec<A> where
A: Send,
<A as Array>::Index: Send,`

### `impl<A> Sync for ArrayVec<A> where
A: Sync,
<A as Array>::Index: Sync,`

## Blanket Implementations

### `impl<T> From<T> for T`[src]

#### `fn from(t: T) -> T`[src]

Performs the conversion.

### `impl<T, U> TryFrom<U> for T where
U: Into<T>,` [src]

#### `type Error = Infallible`

The type returned in the event of a conversion error.

#### `fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>`[src]

Performs the conversion.

### `impl<T, U> Into<U> for T where
U: From<T>,` [src]

#### `fn into(self) -> U`[src]

Performs the conversion.

### `impl<T, U> TryInto<U> for T where
U: TryFrom<T>,` [src]

#### `type Error = <U as TryFrom<T>>::Error`

The type returned in the event of a conversion error.

#### `fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>`[src]

Performs the conversion.

### `impl<I> IntoIterator for I where
I: Iterator,` [src]

#### `type Item = <I as Iterator>::Item`

The type of the elements being iterated over.

#### `type IntoIter = I`

Which kind of iterator are we turning this into?

#### `fn into_iter(self) -> I`[src]

Creates an iterator from a value. Read more

### `impl<T> Borrow<T> for T where
T: ?Sized,` [src]

#### `fn borrow(&self) -> &T`[src]

Immutably borrows from an owned value. Read more

### `impl<T> BorrowMut<T> for T where
T: ?Sized,` [src]

#### `fn borrow_mut(&mut self) -> &mut T`[src]

Mutably borrows from an owned value. Read more

### `impl<T> Any for T where
T: 'static + ?Sized,` [src]

#### `fn type_id(&self) -> TypeId`[src]

Gets the `TypeId` of `self`. Read more

# Help

## Keyboard Shortcuts

`?`
  Show this help dialog
`S`
  Focus the search field
`↑`
  Move up in search results
`↓`
  Move down in search results
`↹`
  Switch tab
`⏎`
  Go to active search result
`+`
  Expand all sections
`-`
  Collapse all sections

## Search Tricks

Prefix searches with a type followed by a colon (e.g., `fn:`) to restrict the search to a given type.

Accepted types are: `fn`, `mod`, `struct`, `enum`, `trait`, `type`, `macro`, and `const`.

Search functions by type signature (e.g., `vec -> usize` or `* -> vec`)

Search multiple things at once by splitting your query with comma (e.g., `str,u8` or `String,struct:Vec,test`)