7
Collections
Scalar types are often not convenient or practical for holding multiple values. In this chapter, we expand the conversation to include collection types, which are ideal for holding values. Imagine creating a financial application that tracks sales for 20 regions. You could create and manage 20 separate scalar variables, one for each region. It is more convenient to create a single variable—a collection of 20 elements. The collection would also be better at managing multiple elements, including sorting, creating reports, and displaying values.
The core collection types in Rust are similar to those in other languages: array, vector, and hash map.
Array type: Array is a fixed-sized collection of values. The values of the array must ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access