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>
impl<T, LenT: LenType, const N: usize> Vec<T, N, LenT>
Sourcepub const fn new() -> Self
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();
Sourcepub fn from_slice(other: &[T]) -> Result<Self, CapacityError>where
T: Clone,
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();
Sourcepub fn from_array<const M: usize>(src: [T; M]) -> Self
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.
Sourcepub fn into_array<const M: usize>(self) -> Result<[T; M], Self>
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]);
Sourcepub fn cast_len_type<NewLenT: LenType>(self) -> Vec<T, N, NewLenT>
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.