pub struct ArrayVec<A: Array> { /* private fields */ }
Expand description
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.
Implementations
sourceimpl<A: Array> ArrayVec<A>
impl<A: Array> ArrayVec<A>
sourcepub fn new() -> ArrayVec<A>
pub fn new() -> ArrayVec<A>
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);
sourcepub fn len(&self) -> usize
pub fn len(&self) -> usize
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);
sourcepub fn is_empty(&self) -> bool
pub fn is_empty(&self) -> bool
Returns whether the ArrayVec
is empty.
use arrayvec::ArrayVec;
let mut array = ArrayVec::from([1]);
array.pop();
assert_eq!(array.is_empty(), true);
sourcepub fn capacity(&self) -> usize
pub fn capacity(&self) -> usize
Return the capacity of the ArrayVec
.
use arrayvec::ArrayVec;
let array = ArrayVec::from([1, 2, 3]);
assert_eq!(array.capacity(), 3);
sourcepub fn is_full(&self) -> bool
pub fn is_full(&self) -> bool
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());
sourcepub fn remaining_capacity(&self) -> usize
pub fn remaining_capacity(&self) -> usize
Returns the capacity left in the ArrayVec
.
use arrayvec::ArrayVec;
let mut array = ArrayVec::from([1, 2, 3]);
array.pop();
assert_eq!(array.remaining_capacity(), 1);
sourcepub fn push(&mut self, element: A::Item)
pub fn push(&mut self, element: A::Item)
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]);
sourcepub fn try_push(
&mut self,
element: A::Item
) -> Result<(), CapacityError<A::Item>>
pub fn try_push(
&mut self,
element: A::Item
) -> Result<(), CapacityError<A::Item>>
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());
sourcepub unsafe fn push_unchecked(&mut self, element: A::Item)
pub unsafe fn push_unchecked(&mut self, element: A::Item)
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]);
sourcepub fn insert(&mut self, index: usize, element: A::Item)
pub fn insert(&mut self, index: usize, element: A::Item)
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"]);
sourcepub fn try_insert(
&mut self,
index: usize,
element: A::Item
) -> Result<(), CapacityError<A::Item>>
pub fn try_insert(
&mut self,
index: usize,
element: A::Item
) -> Result<(), CapacityError<A::Item>>
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"]);
sourcepub fn pop(&mut self) -> Option<A::Item>
pub fn pop(&mut self) -> Option<A::Item>
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);
sourcepub fn swap_remove(&mut self, index: usize) -> A::Item
pub fn swap_remove(&mut self, index: usize) -> A::Item
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]);
sourcepub fn swap_pop(&mut self, index: usize) -> Option<A::Item>
pub fn swap_pop(&mut self, index: usize) -> Option<A::Item>
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);
sourcepub fn remove(&mut self, index: usize) -> A::Item
pub fn remove(&mut self, index: usize) -> A::Item
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]);
sourcepub fn pop_at(&mut self, index: usize) -> Option<A::Item>
pub fn pop_at(&mut self, index: usize) -> Option<A::Item>
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());
sourcepub fn truncate(&mut self, new_len: usize)
pub fn truncate(&mut self, new_len: usize)
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]);
sourcepub fn retain<F>(&mut self, f: F) where
F: FnMut(&mut A::Item) -> bool,
pub fn retain<F>(&mut self, f: F) where
F: FnMut(&mut A::Item) -> bool,
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]);
sourcepub unsafe fn set_len(&mut self, length: usize)
pub unsafe fn set_len(&mut self, length: usize)
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 length
is
not greater than the capacity.
sourcepub fn try_extend_from_slice(
&mut self,
other: &[A::Item]
) -> Result<(), CapacityError> where
A::Item: Copy,
pub fn try_extend_from_slice(
&mut self,
other: &[A::Item]
) -> Result<(), CapacityError> where
A::Item: Copy,
Copy and appends all elements in a slice to the ArrayVec
.
use arrayvec::ArrayVec;
let mut vec: ArrayVec<[usize; 10]> = ArrayVec::new();
vec.push(1);
vec.try_extend_from_slice(&[2, 3]).unwrap();
assert_eq!(&vec[..], &[1, 2, 3]);
Errors
This method will return an error if the capacity left (see
remaining_capacity
) is smaller then the length of the provided
slice.
sourcepub fn drain<R>(&mut self, range: R) -> Drain<'_, A>ⓘNotable traits for Drain<'a, A>impl<'a, A: Array> Iterator for Drain<'a, A> where
A::Item: 'a, type Item = A::Item;
where
R: RangeBounds<usize>,
pub fn drain<R>(&mut self, range: R) -> Drain<'_, A>ⓘNotable traits for Drain<'a, A>impl<'a, A: Array> Iterator for Drain<'a, A> where
A::Item: 'a, type Item = A::Item;
where
R: RangeBounds<usize>,
A::Item: 'a, type Item = A::Item;
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]);
sourcepub fn into_inner(self) -> Result<A, Self>
pub fn into_inner(self) -> Result<A, Self>
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.
sourcepub fn dispose(self)
👎 Deprecated: Use std::mem::drop instead, if at all needed.
pub fn dispose(self)
Use std::mem::drop instead, if at all needed.
Dispose of self
(same as drop)
sourcepub fn as_mut_slice(&mut self) -> &mut [A::Item]
pub fn as_mut_slice(&mut self) -> &mut [A::Item]
Return a mutable slice containing all elements of the vector.
sourcepub fn as_mut_ptr(&mut self) -> *mut A::Item
pub fn as_mut_ptr(&mut self) -> *mut A::Item
Return a raw mutable pointer to the vector’s buffer.
Trait Implementations
sourceimpl<A: Array> Extend<<A as Array>::Item> for ArrayVec<A>
impl<A: Array> Extend<<A as Array>::Item> for ArrayVec<A>
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.
sourcefn extend<T: IntoIterator<Item = A::Item>>(&mut self, iter: T)
fn extend<T: IntoIterator<Item = A::Item>>(&mut self, iter: T)
Extends a collection with the contents of an iterator. Read more
sourcefn extend_one(&mut self, item: A)
fn extend_one(&mut self, item: A)
extend_one
)Extends a collection with exactly one element.
sourcefn extend_reserve(&mut self, additional: usize)
fn extend_reserve(&mut self, additional: usize)
extend_one
)Reserves capacity in a collection for the given number of additional elements. Read more
sourceimpl<A: Array> From<A> for ArrayVec<A>
impl<A: Array> From<A> for ArrayVec<A>
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);
sourceimpl<A: Array> FromIterator<<A as Array>::Item> for ArrayVec<A>
impl<A: Array> FromIterator<<A as Array>::Item> for ArrayVec<A>
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.
sourcefn from_iter<T: IntoIterator<Item = A::Item>>(iter: T) -> Self
fn from_iter<T: IntoIterator<Item = A::Item>>(iter: T) -> Self
Creates a value from an iterator. Read more
sourceimpl<'a, A: Array> IntoIterator for &'a ArrayVec<A>
impl<'a, A: Array> IntoIterator for &'a ArrayVec<A>
Iterate the ArrayVec
with references to each element.
use arrayvec::ArrayVec;
let array = ArrayVec::from([1, 2, 3]);
for elt in &array {
// ...
}
sourceimpl<'a, A: Array> IntoIterator for &'a mut ArrayVec<A>
impl<'a, A: Array> IntoIterator for &'a mut ArrayVec<A>
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 {
// ...
}
sourceimpl<A: Array> IntoIterator for ArrayVec<A>
impl<A: Array> IntoIterator for ArrayVec<A>
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]) {
// ...
}
sourceimpl<A: Array> Ord for ArrayVec<A> where
A::Item: Ord,
impl<A: Array> Ord for ArrayVec<A> where
A::Item: Ord,
sourceimpl<A: Array> PartialOrd<ArrayVec<A>> for ArrayVec<A> where
A::Item: PartialOrd,
impl<A: Array> PartialOrd<ArrayVec<A>> for ArrayVec<A> where
A::Item: PartialOrd,
sourcefn partial_cmp(&self, other: &ArrayVec<A>) -> Option<Ordering>
fn partial_cmp(&self, other: &ArrayVec<A>) -> Option<Ordering>
This method returns an ordering between self
and other
values if one exists. Read more
sourcefn lt(&self, other: &Self) -> bool
fn lt(&self, other: &Self) -> bool
This method tests less than (for self
and other
) and is used by the <
operator. Read more
sourcefn le(&self, other: &Self) -> bool
fn le(&self, other: &Self) -> bool
This method tests less than or equal to (for self
and other
) and is used by the <=
operator. Read more
sourceimpl<A: Array> TryFrom<&'_ [<A as Array>::Item]> for ArrayVec<A> where
A::Item: Clone,
impl<A: Array> TryFrom<&'_ [<A as Array>::Item]> for ArrayVec<A> where
A::Item: Clone,
Try to create an ArrayVec
from a slice. This will return an error if the slice was too big to
fit.
use arrayvec::ArrayVec;
use std::convert::TryInto as _;
let array: ArrayVec<[_; 4]> = (&[1, 2, 3] as &[_]).try_into().unwrap();
assert_eq!(array.len(), 3);
assert_eq!(array.capacity(), 4);
impl<A: Array> Eq for ArrayVec<A> where
A::Item: Eq,
Auto Trait Implementations
impl<A> RefUnwindSafe for ArrayVec<A> where
A: RefUnwindSafe,
<A as Array>::Index: RefUnwindSafe,
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,
impl<A> Unpin for ArrayVec<A> where
A: Unpin,
<A as Array>::Index: Unpin,
impl<A> UnwindSafe for ArrayVec<A> where
A: UnwindSafe,
<A as Array>::Index: UnwindSafe,
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcepub fn borrow_mut(&mut self) -> &mut T
pub fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more