| Crates.io | stack_collections |
| lib.rs | stack_collections |
| version | 0.3.2 |
| created_at | 2025-10-02 13:29:11.482162+00 |
| updated_at | 2025-11-19 16:36:31.109493+00 |
| description | Stack-allocated collections for Rust |
| homepage | https://github.com/OverwrittenCode/stack_collections |
| repository | https://github.com/OverwrittenCode/stack_collections |
| max_upload_size | |
| id | 1864362 |
| size | 140,021 |
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 for StackVec<StackString<N>, CAP>const fn constructors and many operationsIntoIterator, DoubleEndedIterator, and ExactSizeIteratorDebug, Display, Clone, Hash, PartialEq, Eq, Ord, etc.Write trait implementation for StackStringAdd this to your Cargo.toml:
[dependencies]
stack_collections = "0.3.2"
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:
no_std environments (note: currently requires std for some traits)Both StackString and StackVec provide:
push_unchecked, pop_unchecked, etc. for performance-critical codetry_push, try_pop, etc. that return Option instead of panickingpush, pop, insert, remove, clear, truncate, etc.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:
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.