Stack-allocated collections for Rust: fixed-capacity string and vector types that live entirely on the stack.
StackString<N>
: UTF-8 encoded, fixed-capacity string stored on the stackStackVec<T, CAP>
: Fixed-capacity vector stored on the stackStackArrayString<N, CAP>
: Convenience alias forStackVec<StackString<N>, CAP>
- Zero heap allocations
const fn
constructors and many operations- Comprehensive API with safe and unsafe variants
- Full iterator support including
IntoIterator
,DoubleEndedIterator
, andExactSizeIterator
- Standard trait implementations:
Debug
,Display
,Clone
,Hash
,PartialEq
,Eq
,Ord
, etc. Write
trait implementation forStackString
Add this to your Cargo.toml
:
[dependencies]
stack_collections = "0.3.1"
use stack_collections::StackString;
fn main() {
let mut stack_string = StackString::<32>::new();
stack_string.push_str("Hello, ");
stack_string.push_str("world!");
println!("String: {}", stack_string);
println!("Length: {}", stack_string.len());
println!("Capacity: {}", stack_string.capacity());
// Pop characters
while let Some(c) = stack_string.try_pop() {
println!("Popped: {}", c);
}
}
use stack_collections::StackVec;
fn main() {
let mut vec = StackVec::<i32, 8>::new();
for i in 1..=5 {
vec.push(i);
}
println!("Vector: {:?}", vec);
println!("Sum: {}", vec.iter().sum::<i32>());
vec.retain(|x| *x < 4);
println!("Numbers less than 4: {:?}", vec);
}
use stack_collections::StackArrayString;
fn main() {
// StackArrayString is a convenient alias for StackVec<StackString<N>, CAP>
let mut arr: StackArrayString<16, 4> = StackArrayString::new();
arr.push("hello".try_into().unwrap());
arr.push("world".try_into().unwrap());
assert_eq!(arr.len(), 2);
assert_eq!(arr.capacity(), 4);
assert_eq!(arr[0].capacity(), 16);
assert_eq!(arr[0].as_str(), "hello");
assert_eq!(arr[1].as_str(), "world");
}
Use stack_collections
when you:
- Need predictable memory usage without heap allocations
- Know the maximum capacity at compile time
- Want to avoid allocator overhead for small collections
- Are working in
no_std
environments (note: currently requiresstd
for some traits) - Need deterministic performance without allocator variance
Both StackString
and StackVec
provide:
- Unchecked variants:
push_unchecked
,pop_unchecked
, etc. for performance-critical code - Try variants:
try_push
,try_pop
, etc. that returnOption
instead of panicking - Standard methods:
push
,pop
,insert
,remove
,clear
,truncate
, etc. - Deref to slice/str: Seamless integration with standard library functions
This crate uses unsafe
internally for performance but exposes a safe API. All unsafe operations are carefully documented and validated. The public API is designed to prevent
undefined behavior even when capacity is exceeded (operations will panic or return None
instead).
Licensed under either of:
- Apache License, Version 2.0 (LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0)
- MIT license (LICENSE-MIT or http://opensource.org/licenses/MIT)
at your option.
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.