Data Structures

cat~data-structures

Common data structures include arrays and vectors, which are contiguous block of memory that stores elements of the same data type; stacks ("last-in, first-out" (LIFO) data structures); queues ("first-in, first-out" (FIFO) data structures); linked lists (sequences of nodes, where each node contains data and a pointer to the next node); maps (aka dictionaries, which store key-value pairs); sets (collections of unique elements); trees (hierarchical data structures consisting of nodes with parent-child relationships); and graphs.

The std::collections module of the standard library includes:

  • Vec<T> (Vector) is a dynamic array that can grow or shrink as needed. It's the most commonly used collection in Rust and is similar to a dynamic array or list in other languages. It provides fast access by index.
  • HashMap<K, V> (Hash Map) stores key-value pairs, allowing for efficient lookup of values based on their keys. It uses a hash function for fast average-case access.
  • BTreeMap<K, V> (B-Tree Map) is similar to HashMap, but it keeps the keys sorted. It provides ordered access to key-value pairs.
  • HashSet<T> (Hash Set) stores a collection of unique elements. Used for efficiently checking membership and ensuring uniqueness.
  • BTreeSet<T> (B-Tree Set) is similar to HashSet, but it keeps the elements sorted.
  • LinkedList<T> is a doubly linked list. Useful for frequent insertions and deletions at arbitrary positions, but less efficient for random access.
  • VecDeque<T> (Vector Deque) is a double-ended queue. Allows efficient insertion and deletion at both ends.
  • BinaryHeap<T> is a binary heap, often used to implement a priority queue.
  • String is a growable, UTF-8 encoded string. It's a fundamental data structure for working with text.

Refer to the Language and Standard Library sections for examples of use.

Strings

Vectors

Stacks and Queues

Stack-allocated Arrays

Hashmaps

B-Trees

HashMap's Friends: IndexMap, MultiMap, and SlotMap

Binary Heaps / Priority Queues

Graphs

Linked Lists

RecipeCratesCategories
Store Data in a Linked Liststdcat~data-structures

Bit Arrays

Heapless Data Structures

Unique Identifiers

Additional Data Structures

  • Data Types.
  • Smart Pointers.
  • Immutable Data Structures: im, rpds. See Functional Programming.
  • Specialized Data Structures:
    • Trees and Tries: indextree.
    • Bloom Filters: bloomfilter.
    • Skip Lists: skiplist.
    • Ranges as keys: rangemap stores key-value pairs where keys are ranges.
    • Matrices and Tensors: ndarray provides an n-dimensional array for numerical computation. See Linear Algebra.
  • Serialization / Deserialization.