A no-std, stack-allocated vector with fixed capacity and dynamic length: static_vector::Vec::<T, CAPACITY>
.
[Vec
] stores elements on the stack using a fixed-size array without heap allocations.
Aims to be suitable for low-level projects and to have an API as safe and explicit as possible. The goal is to allocate only when needed. When first constructed, the vector will not allocate.
Note: It's a learning project, so there are no guarantees.
- No heap allocation (
#![no_std]
compatible) - Supports iteration, mutable access, clearing, resizing
- Compile-time enforced capacity
CAPACITY
> 0, otherwise [Vec::new()
] panicsT: Default
only if [Vec::set_len()
] is used
All operations are O(1) except:
Method | Time Complexity | Space Complexity |
---|---|---|
clear |
O(current length) | O(1) |
set_len |
O(new length - current length) | O(new length - current length) |
extend_from_slice |
O(slice length) | O(slice length) |
append |
O(other vector length) | O(other vector length) |
cargo add static_vector --git https://github.com/andreiavrammsd/static_vector.rs
This crate is not published on crates.io.
use static_vector::vec;
// Create a static vector with capacity 3, length 3, and elements 1, 2, 3.
let mut vec = vec![1, 2, 3];
vec.pop().unwrap();
assert_eq!(vec.len(), 2);
vec.push(4).unwrap();
assert_eq!(vec.len(), 3);
assert_eq!(vec.last(), Some(&4));
assert_eq!(vec.as_slice(), &[1, 2, 4]);
let sum_of_even_numbers = vec.iter().filter(|n| *n % 2 == 0).sum::<i32>();
assert_eq!(sum_of_even_numbers, 6);
vec.push(2).unwrap_err();
assert_eq!(vec.len(), 3);
match vec.set_len(1) {
Ok(()) => assert_eq!(vec.len(), 1),
Err(err) => eprintln!("{:?}", err),
}
vec.clear();
assert!(vec.is_empty());
See more examples in the documentation of [Vec
] and in examples.