-
Book Overview & Buying
-
Table Of Contents
Rust for C++ Developers
By :
Just as C++ has std::array, std::vector, and std::span for storing contiguous elements that can be trivially indexed, Rust has arrays, vectors, and slices, respectively. In this section, we'll examine these elements of Rust and learn how they work together while preserving Rust's borrowing rules.
We've already seen the syntax for declaring arrays in Chapter 2, and in Chapter 3, we learned that out-of-bounds access will cause a panic to enforce Rust's safety properties. Now, we'll focus on how to use Rust arrays in practice.
Arrays choose a fixed size at initialization that will never change during the runtime of a program. The size must also be known at compile time, meaning we can't, for instance, read a file of unknown size and then present the file's data to the rest of our program as an array.
Arrays are also a generic type, like all the indexed types we'll look at in this section...