Chapter 18: Common Collection Types

A collection type in Rust is a data structure that can store multiple elements dynamically at runtime. Collections, such as Vec, String, and HashMap, are provided by the standard library to handle scenarios where data size, structure, or content may vary. Unlike arrays and tuples—built-in constructs with fixed sizes—collections can grow, shrink, or otherwise adapt to your program's needs.

All standard library collection types are generic. For instance, a Vec<T> is defined for some type T, a HashMap<K, V> for some key type K and value type V, and so on. These generics allow collections to be reused with different data types while providing strong compile-time type checking.

Arrays and tuples are efficient for fixed-size data. However, their static nature makes them less useful when handling data that must change in size or structure. Dynamic collections like vectors (Vec<T>) fill this gap, offering flexible, safe, and efficient data manipulation, while maintaining Rust's strict memory safety guarantees.

This chapter introduces Rust's most commonly used collection types, compares them to fixed-size built-in arrays and tuples, and explains their advantages for effectively managing dynamic and complex data.