Collections

Rust's standard library contains some common collection types, such as sequences, sets, and maps. These live in the module. In  environments, they can be accessed under.

All collections are owned and allocated on the heap.

Sequences
Sequences can contain an arbitrary number of items of the same type. The standard library contains three sequence types:,  , and.

Vec
is the most ubiquitous collection type. It is backed by an array. When items are added or removed from the, the array grows or shrinks dynamically. Even though pushing items to a  sometimes requires copying the array, the amortized time to push an item is constant. is the most performant sequence type in the standard library for many purposes, and the only one that can be sliced.

VecDeque
is a circular buffer. It works similar to, except that inserting or removing items at the start of the sequence is more efficient.

LinkedList
is a doubly-linked list. It allows adding and removing items at both ends in constant time, but doesn't allow indexing items in constant time. is less memory efficient and less cache friendly than  and , which very often leads to worse performance.

Maps
Maps are data structures that map keys to values. The standard library contains two map types:  and.

HashMap


TODO

BTreeMap


TODO

Sets
Sets are unordered collections that can't contain the same item more than once. The standard library contains the and  types, which internally use the same implementation as   and   (the items are stored in the keys, and the values are empty tuples).

BinaryHeap
The is a priority queue implemented with a binary heap.