Vec

Type Alias Vec 

Source
pub type Vec<T, const N: usize, LenT = usize> = VecInner<T, LenT, OwnedVecStorage<T, N>>;
Expand description

A fixed capacity Vec.

§Examples

use heapless::Vec;

// A vector with a fixed capacity of 8 elements allocated on the stack
let mut vec = Vec::<_, 8>::new();
vec.push(1).unwrap();
vec.push(2).unwrap();

assert_eq!(vec.len(), 2);
assert_eq!(vec[0], 1);

assert_eq!(vec.pop(), Some(2));
assert_eq!(vec.len(), 1);

vec[0] = 7;
assert_eq!(vec[0], 7);

vec.extend([1, 2, 3].iter().cloned());

for x in &vec {
    println!("{}", x);
}
assert_eq!(*vec, [7, 1, 2, 3]);

In some cases, the const-generic might be cumbersome. Vec can coerce into a VecView to remove the need for the const-generic:

use heapless::{Vec, VecView};

let vec: Vec<u8, 10> = Vec::from_slice(&[1, 2, 3, 4]).unwrap();
let view: &VecView<_, _> = &vec;

For uncommmon capacity values, or in generic scenarios, you may have to provide the LenT generic yourself.

This should be the smallest unsigned integer type that your capacity fits in, or usize if you don’t want to consider this.

Aliased Type§

pub struct Vec<T, const N: usize, LenT = usize> { /* private fields */ }

Implementations§

Source§

impl<T, LenT: LenType, const N: usize> Vec<T, N, LenT>

Source

pub const fn new() -> Self

Constructs a new, empty vector with a fixed capacity of N

§Examples
use heapless::Vec;

// allocate the vector on the stack
let mut x: Vec<u8, 16> = Vec::new();

// allocate the vector in a static variable
static mut X: Vec<u8, 16> = Vec::new();
Source

pub fn from_slice(other: &[T]) -> Result<Self, CapacityError>
where T: Clone,

Constructs a new vector with a fixed capacity of N and fills it with the provided slice.

This is equivalent to the following code:

use heapless::Vec;

let mut v: Vec<u8, 16> = Vec::new();
v.extend_from_slice(&[1, 2, 3]).unwrap();
Source

pub fn from_array<const M: usize>(src: [T; M]) -> Self

Constructs a new vector with a fixed capacity of N, initializing it with the provided array.

The length of the provided array, M may be equal to or less than the capacity of the vector, N.

If the length of the provided array is greater than the capacity of the vector a compile-time error will be produced.

Source

pub fn into_array<const M: usize>(self) -> Result<[T; M], Self>

Returns the contents of the vector as an array of length M if the length of the vector is exactly M, otherwise returns Err(self).

§Examples
use heapless::Vec;
let buffer: Vec<u8, 42> = Vec::from_slice(&[1, 2, 3, 5, 8]).unwrap();
let array: [u8; 5] = buffer.into_array().unwrap();
assert_eq!(array, [1, 2, 3, 5, 8]);
Source

pub fn cast_len_type<NewLenT: LenType>(self) -> Vec<T, N, NewLenT>

Casts the LenT type to a new type, preserving everything else about the vector.

This can be useful if you need to pass a Vec<T, N, u8> into a Vec<T, N, usize> for example.

This will check at compile time if the N value will fit into NewLenT, and error if not.

Trait Implementations§

Source§

impl<T, const N: usize, LenT: LenType> Clone for Vec<T, N, LenT>
where T: Clone,

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<T, LenT: LenType, const N: usize> Default for Vec<T, N, LenT>

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl<T, LenT: LenType, const N: usize, const M: usize> From<[T; M]> for Vec<T, N, LenT>

Source§

fn from(array: [T; M]) -> Self

Converts to this type from the input type.
Source§

impl<T, LenT: LenType, const N: usize> FromIterator<T> for Vec<T, N, LenT>

Source§

fn from_iter<I>(iter: I) -> Self
where I: IntoIterator<Item = T>,

Creates a value from an iterator. Read more
Source§

impl<T, LenT: LenType, const N: usize> IntoIterator for Vec<T, N, LenT>

Source§

type Item = T

The type of the elements being iterated over.
Source§

type IntoIter = IntoIter<T, N, LenT>

Which kind of iterator are we turning this into?
Source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
Source§

impl<'a, T: Clone, LenT: LenType, const N: usize> TryFrom<&'a [T]> for Vec<T, N, LenT>

Source§

type Error = CapacityError

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

fn try_from(slice: &'a [T]) -> Result<Self, Self::Error>

Performs the conversion.