pub struct ArrayVec<T, const N: usize> { /* private fields */ }Expand description
A fixed-size array, which has vector-like operations.
Implementations§
Source§impl<T, const N: usize> ArrayVec<T, N>
impl<T, const N: usize> ArrayVec<T, N>
Sourcepub const fn new() -> ArrayVec<T, N>
pub const fn new() -> ArrayVec<T, N>
Creates a new ArrayVec with no elements.
§Examples
use no_alloc::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 len(&self) -> usize
pub fn len(&self) -> usize
Returns the number of elements in the ArrayVec.
§Examples
use no_alloc::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 no_alloc::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 no_alloc::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 no_alloc::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 no_alloc::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 no_alloc::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 no_alloc::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 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 no_alloc::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>where
T: Copy,
impl<T, const N: usize> Index<usize> for ArrayVec<T, N>where
T: Copy,
Source§fn index(&self, index: usize) -> &<ArrayVec<T, N> as Index<usize>>::Output
fn index(&self, index: usize) -> &<ArrayVec<T, N> as Index<usize>>::Output
Returns a reference to the element at the given index.
§Panics
Panics if the index is out of bounds.
§Examples
use no_alloc::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>where
T: Copy,
impl<T, const N: usize> IndexMut<usize> for ArrayVec<T, N>where
T: Copy,
Source§fn index_mut(
&mut self,
index: usize,
) -> &mut <ArrayVec<T, N> as Index<usize>>::Output
fn index_mut( &mut self, index: usize, ) -> &mut <ArrayVec<T, N> as Index<usize>>::Output
Returns a mutable reference to the element at the given index.
§Panics
Panics if the index is out of bounds.
§Examples
use no_alloc::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);