Arrays
We’ve spent a lot of time with strings. That makes sense, because we want our programs to interact with human beings, and human beings tend to like words and sentences. We can logically think about strings as a sequence of characters. However, strings are just one kind of a sequence of values. What if we want a sequence of numbers, or people, or something else?
In this lesson, we’re going to learn about sequences of generic values. We’ll start off with arrays, which live on the stack. We’ll talk about Vec
s (short for vectors), which are dynamically allocated like String
s. We’ll learn about slices and see how they relate to the string slices we’ve been using up until now.
Let’s get to it! Time to learn about a new expression, array expressions. We write these with square brackets containing a comma-separated list of values.