pub struct ArrayVec<T, const N: usize> { /* private fields */ }Expand description
A fixed-size array, which has vector-like operations.
ArrayVec provides a vector-like interface with a compile-time fixed capacity N.
Elements are stored in a stack-allocated array, making it suitable for no_std
environments and situations where heap allocation is not available or desirable.
§Type Parameters
T: The type of elements stored in the vectorN: The maximum number of elements (capacity)
§Examples
use planck_noalloc::vec::ArrayVec;
// Create a vector with capacity 4
let mut vec = ArrayVec::<String, 4>::new();
vec.push(String::from("hello"));
vec.push(String::from("world"));
assert_eq!(vec.len(), 2);Implementations§
Source§impl<T, const N: usize> ArrayVec<T, N>
impl<T, const N: usize> ArrayVec<T, N>
Sourcepub const fn new() -> Self
pub const fn new() -> Self
Creates a new ArrayVec with no elements.
§Examples
use planck_noalloc::vec::ArrayVec;
let mut vec = ArrayVec::<u8, 4>::new();
assert_eq!(vec.len(), 0);
assert!(vec.is_empty());Sourcepub fn try_push(&mut self, value: T) -> Result<(), ArrayVecError>
pub fn try_push(&mut self, value: T) -> Result<(), ArrayVecError>
Sourcepub fn swap_remove(&mut self, index: usize) -> T
pub fn swap_remove(&mut self, index: usize) -> T
Sourcepub fn len(&self) -> usize
pub fn len(&self) -> usize
Returns the number of elements in the ArrayVec.
§Examples
use planck_noalloc::vec::ArrayVec;
let mut vec = ArrayVec::<u8, 4>::new();
assert_eq!(vec.len(), 0);
vec.push(1);
assert_eq!(vec.len(), 1);
vec.push(2);
assert_eq!(vec.len(), 2);Sourcepub fn is_empty(&self) -> bool
pub fn is_empty(&self) -> bool
Returns true if the ArrayVec is empty.
§Examples
use planck_noalloc::vec::ArrayVec;
let mut vec = ArrayVec::<u8, 4>::new();
assert!(vec.is_empty());
vec.push(1);
assert!(!vec.is_empty());Sourcepub fn as_slice(&self) -> &[T]
pub fn as_slice(&self) -> &[T]
Returns a slice of all the elements in the ArrayVec.
§Examples
use planck_noalloc::vec::ArrayVec;
let mut vec = ArrayVec::<u8, 4>::new();
vec.push(1);
vec.push(2);
vec.push(3);
vec.push(4);
assert_eq!(vec.as_slice(), &[1, 2, 3, 4]);Sourcepub fn as_mut_slice(&mut self) -> &mut [T]
pub fn as_mut_slice(&mut self) -> &mut [T]
Returns a mutable slice of all the elements in the ArrayVec.
§Examples
use planck_noalloc::vec::ArrayVec;
let mut vec = ArrayVec::<u8, 4>::new();
vec.push(1);
vec.push(2);
vec.push(3);
vec.push(4);
assert_eq!(vec.as_mut_slice(), &mut [1, 2, 3, 4]);Sourcepub fn iter(&self) -> Iter<'_, T>
pub fn iter(&self) -> Iter<'_, T>
Returns an iterator over the elements of the ArrayVec.
§Examples
use planck_noalloc::vec::ArrayVec;
let mut vec = ArrayVec::<u8, 4>::new();
vec.push(1);
vec.push(2);
vec.push(3);
vec.push(4);
for i in vec.iter() {
assert!(*i == 1 || *i == 2 || *i == 3 || *i == 4);
// do something
}Sourcepub fn iter_mut(&mut self) -> IterMut<'_, T>
pub fn iter_mut(&mut self) -> IterMut<'_, T>
Returns a mutable iterator over the elements of the ArrayVec.
§Examples
use planck_noalloc::vec::ArrayVec;
let mut vec = ArrayVec::<u8, 4>::new();
vec.push(1);
vec.push(2);
vec.push(3);
vec.push(4);
for i in vec.iter_mut() {
*i += 1;
// do something
}Sourcepub fn reverse(&mut self)
pub fn reverse(&mut self)
Reverses the order of the elements in the ArrayVec.
§Examples
use planck_noalloc::vec::ArrayVec;
let mut vec = ArrayVec::<u8, 4>::new();
vec.push(1);
vec.push(2);
vec.push(3);
vec.push(4);
vec.reverse();
assert_eq!(vec.as_slice(), &[4, 3, 2, 1]);Sourcepub fn insert(&mut self, index: usize, value: T)
pub fn insert(&mut self, index: usize, value: T)
Inserts value at index, shifting all elements after it to the right.
§Panics
Panics if index > len or if the ArrayVec is full.
Sourcepub fn remove(&mut self, index: usize) -> T
pub fn remove(&mut self, index: usize) -> T
Removes and returns the element at index, shifting all elements
after it to the left. Preserves ordering.
§Panics
Panics if index >= len.
Sourcepub fn as_mut_ptr(&mut self) -> *mut T
pub fn as_mut_ptr(&mut self) -> *mut T
Returns a mutable pointer to the underlying data.
Source§impl<T, const U: usize> ArrayVec<T, U>
impl<T, const U: usize> ArrayVec<T, U>
Sourcepub fn sort_unstable(&mut self)
pub fn sort_unstable(&mut self)
Sorts the ArrayVec in place.
The sort is not stable, meaning that the relative order of elements that are equal is not
preserved.
§Examples
use planck_noalloc::vec::ArrayVec;
let mut vec = ArrayVec::<u8, 4>::new();
vec.push(1);
vec.push(2);
vec.push(3);
vec.push(4);
vec.sort_unstable();
assert_eq!(vec.as_slice(), &[1, 2, 3, 4]);Trait Implementations§
Source§impl<T, const N: usize> Index<usize> for ArrayVec<T, N>
impl<T, const N: usize> Index<usize> for ArrayVec<T, N>
Source§fn index(&self, index: usize) -> &Self::Output
fn index(&self, index: usize) -> &Self::Output
Returns a reference to the element at the given index.
§Panics
Panics if the index is out of bounds.
§Examples
use planck_noalloc::vec::ArrayVec;
let mut vec = ArrayVec::<u8, 4>::new();
vec.push(1);
vec.push(2);
vec.push(3);
vec.push(4);
assert_eq!(vec[0], 1);
assert_eq!(vec[1], 2);
assert_eq!(vec[2], 3);
assert_eq!(vec[3], 4);Source§impl<T, const N: usize> IndexMut<usize> for ArrayVec<T, N>
impl<T, const N: usize> IndexMut<usize> for ArrayVec<T, N>
Source§fn index_mut(&mut self, index: usize) -> &mut Self::Output
fn index_mut(&mut self, index: usize) -> &mut Self::Output
Returns a mutable reference to the element at the given index.
§Panics
Panics if the index is out of bounds.
§Examples
use planck_noalloc::vec::ArrayVec;
let mut vec = ArrayVec::<u8, 4>::new();
vec.push(1);
vec.push(2);
vec.push(3);
vec.push(4);
assert_eq!(vec[0], 1);
assert_eq!(vec[1], 2);
assert_eq!(vec[2], 3);
assert_eq!(vec[3], 4);
vec[0] = 5;
assert_eq!(vec[0], 5);