smallvec is probably one of the most underrated Rust crates
This title could be clearer and more informative.Try out Clickbait Shieldfor free (5 uses left this month).
The `smallvec` crate provides `SmallVec<T, N>`, a drop-in replacement for `Vec<T>` that stores up to N elements inline on the stack, avoiding heap allocation. This makes it valuable for embedded systems where heap fragmentation or absence of heap is a concern, while still gracefully falling back to heap allocation for larger inputs. By choosing an appropriate N, the same data structure can work allocation-free on microcontrollers and handle arbitrary sizes on servers.
Questions this post answers
What is SmallVec in Rust and how does it differ from Vec?
SmallVec<T, N> is a Rust type from the `smallvec` crate that behaves like `Vec<T>` but stores up to N elements inline, typically on the stack, without heap allocation. When the number of elements exceeds N, it falls back to heap allocation just like a regular Vec. This makes it ideal for cases where the common path involves small, bounded collections and heap allocation is expensive or unavailable. Rust developers optimizing for embedded or low-allocation targets track crates like smallvec on daily.dev.
How do I write a Rust data structure that avoids heap allocation on embedded systems but still works on servers?
Use SmallVec<T, N> with an N sized to the maximum expected element count on the constrained system. For example, `SmallVec<u8, 16>` for a token ID field stores up to 16 bytes on the stack with zero heap allocation, while transparently spilling to the heap for longer IDs on general-purpose systems. The same struct compiles and runs correctly in both environments without code changes. Engineers shipping Rust libraries that target both microcontrollers and servers find relevant patterns like this on daily.dev.