Ben Chuanlong Du's Blog

It is never too late to learn.

Summary of Collections in Rust

Things on this page are fragmentary and immature notes/thoughts of the author. Please read with your own judgement!

The module std::collections has a good summary on popular collection data structures in Rust and when to use them.

  • Sequences: Vec, VecDeque (double-ended queue), LinkedList (doubly-linked list)
  • Maps: HashMap, BTreeMap (sorted map)
  • Sets: HashSet, BTreeSet (sorted set)
  • Misc: BinaryHeap (priority queue)

rust-collection-summary

Array in Rust

Graph

Maps in Rust

Please refer to Map in Rust for detailed discussions.

elsa

Elsa provides append-only collections for Rust where borrows to entries can outlive insertions

itertools

itertools provides extra iterator adaptors, iterator methods, free functions, and macros.

Heapless / Stack / Array

heapless / arrayvec: stores on the stack only. Limited capacity, capacity set at creation.

  • heapless provides static friendly data structures that don't require dynamic memory allocation.

  • arrayvec provides a vector with fixed capacity, backed by an array (which is stored on the stack). Notice that you cannot collect an iterator into an Array. However, you can collect an iterator into an ArrayVec. For more discussions, please refer to How do I collect into an array? .

  • smallvec stores some elements on the stack, falls back to the heap if the stack capacity is exceeded.

  • tinyvec provides both, implemented in 100% safe code. For example, smallvec had 5 memory safety bugs to date; they are guaranteed to never happen with tinyvec. The drawback is that the stack storage is zero-initialized on creation, which incurs some cost; for small capacities it's usually negligible, but adds up if you want to store thousands of elements.

  • ndarray is an n-dimensional array for general elements and for numerics. Lightweight array views and slicing; views support chunking and splitting. Array in Rust has discussions on ways to make it easy to construct arrays.

trie

  • trie-hard is a novel implementation of a Trie data structure optimized for small, sparse maps.

References

Contiguous Data in Rust https://github.com/paulkernfeld/contiguous-data-in-rust

Comments