Array and tuple expressions

An array is a sequential collection of data values. There are many ways to use and manipulate them, but here we're interested in the specialized expressions that create them and access their internal data values. To tell Rust that we want to create a new array, all we have to do is write a [ symbol, and then a comma-separated list of expressions that produce the values we want to store in the array, and then a ] symbol. There doesn't have to be anything between the beginning and ending symbols if we want an empty array. So, we can write [] as an expression producing an empty array, or [1, 3, 5] as an expression producing an array containing three numbers. All of the values stored in an array need to have the same data type—integers in this case—so trying to set the second element to a text string such as "nope" would produce a compiler error when we tried to compile the program.

That's really nice for cases where we need to create a short array, but imagine writing out an expression for an array containing a thousand values! We wouldn't want to write them out one by one. Fortunately, we can instead do something like [0; 1000], which produces an array containing a thousand zero values. Some other part of our code can then fill in different values in those slots.

Once we have an array value, we often need access to the values stored inside it. That too is achieved using the [ and ] symbols. If we have an array named an_array (we'll see how to give values names in the Variables, types, and mutability section of this chapter), we can access the first value in the array as an_array[0], the second value as an_array[1], and so on. Notice that the first value is numbered with 0, while the second is 1. Many programming languages count this way, because it simplifies some of the math that they frequently need to do with respect to arrays and other sequences of values.

In addition to arrays, Rust allows us to make tuples. The expression to create a tuple is similar to that for arrays: (1, "wow", true) is a tuple containing the number value 1, the text value wow, and the Boolean true. If we have a tuple named a_tuple, then a_tuple.1 produces the second value in the tuple, in this case the word wow. There's no simplified way to create a tuple containing a thousand duplicates, though, because that's not what they're for. Unlike arrays, a single tuple can contain values of more than one value type, and they are intended to serve as lightweight data structures, rather than as a collection of many similar data values.

In some languages, the contents of a tuple cannot be changed. That's not how it works in Rust, though, where tuples follow the same rules for that sort of thing as any other data structure.

If we need to make a tuple with only one contained value (which is not common, because the whole point of a tuple is to associate multiple values together), we need to include a comma after the value. So, a single-element tuple containing the number 5 looks like this: (5,)

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.191.215.96