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 ...

Get Programming with Rust now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.