Expand description
A heap-allocated, fixed-capacity, variable-size array, no_std compatible.
CapVec<T, N> is a heap-allocated container that stores up to N elements
of type T contiguously in memory.
Unlike [Vec], the capacity is fixed at compile-time, and cannot grow.
However, its length (len) can vary dynamically up to N.
This type is ideal for cases where:
- You’re working in
no_stdenvironments. - You want a
Vec-like API but need deterministic capacity. - You prefer heap allocation over stack-based fixed arrays like
[T; N].
use cap_vec::CapVec;
let mut v = CapVec::<i32, 4>::new();
assert!(v.is_empty());
v.push(10).unwrap();
v.push(20).unwrap();
v.push(30).unwrap();
assert_eq!(v.len(), 3);
assert_eq!(v.first(), Some(&10));
assert_eq!(v.get(1), Some(&20));
assert_eq!(v.last(), Some(&30));
assert_eq!(&v[..], &[10, 20, 30]);
assert_eq!(v.remove(1), Some(20));
assert_eq!(v.pop(), Some(30));
assert_eq!(v.pop(), Some(10));