Skip to main content

ArrayVec

Struct ArrayVec 

Source
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>

Source

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());
Source

pub fn try_push(&mut self, value: T) -> Result<(), ArrayVecError>

Tries to push a value into the ArrayVec.

§Errors

Returns an error if the ArrayVec is full.

§Examples
use no_alloc::vec::ArrayVec;

let mut vec = ArrayVec::<u8, 1>::new();
assert!(vec.try_push(1).is_ok());
assert_eq!(vec.len(), 1);
assert!(vec.try_push(2).is_err());
Source

pub fn push(&mut self, value: T)

Pushes a value into the ArrayVec.

§Panics

Panics if the ArrayVec is full.

Source

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);
Source

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());
Source

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]);
Source

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]);
Source

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
}
Source

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
}
Source

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]);
Source

pub fn as_ptr(&self) -> *const T

Returns a pointer to the underlying data.

Source

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>
where T: Copy + PartialEq,

Source

pub fn contains(&self, value: &T) -> bool

Returns true if the ArrayVec contains the given value.

§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!(vec.contains(&1));
assert!(!vec.contains(&5));
Source§

impl<T, const U: usize> ArrayVec<T, U>
where T: Copy + Ord,

Source

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> Debug for ArrayVec<T, N>
where T: Debug,

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
Source§

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

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§

type Output = T

The returned type after indexing.
Source§

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

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);

Auto Trait Implementations§

§

impl<T, const N: usize> Freeze for ArrayVec<T, N>
where T: Freeze,

§

impl<T, const N: usize> RefUnwindSafe for ArrayVec<T, N>
where T: RefUnwindSafe,

§

impl<T, const N: usize> Send for ArrayVec<T, N>
where T: Send,

§

impl<T, const N: usize> Sync for ArrayVec<T, N>
where T: Sync,

§

impl<T, const N: usize> Unpin for ArrayVec<T, N>
where T: Unpin,

§

impl<T, const N: usize> UnsafeUnpin for ArrayVec<T, N>
where T: UnsafeUnpin,

§

impl<T, const N: usize> UnwindSafe for ArrayVec<T, N>
where T: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

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

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.