Metadata
-
Date
-
Tagged
-
Older post
-
Newer post
Rust: tuple vs array
The array and the tuple are both compound types, that means they can hold multiple values. An array and a tuple are both groups of other values.
Tuples are enclosed by parentheses ()
.
Every element in it is separated by a comma ,
.
Arrays are enclosed by square braces []
.
Every element in it is separated by a comma ,
.
They are both fixed in size, they can’t shrink or grow. A vector is a similar collection type that is allowed to change in size.
So far, they sound very similar, with just some arbitrary syntax differentiating them.
Types
For an array, every item in that group has to have the same type.
If a value in that array is of type f64
, every item in that array must be of type f64
.
For tuples, items in that group can have different types. All values in a tuple can be the same type, but they can be different too.
Annotating the types
The Rust compiler is smart and will try to figure out which types it’s dealing with, that means explicit type declarations are not always needed.
For arrays, the type declaration consists of enclosing square brackets. Inside is the type of the elements followed by a semicolon ;
, and the length or the array.
For tuples, the type declaration consists of enclosing parentheses. Inside are the types of each element, each one separated by a comma ,
.
Accessing information
Element by index
Indexes for both tuples and arrays start at 0.
Tuples, somewhat surprisingly, use the .
to access an element at a specific index.
Element by destructuring
It’s a lot more common to destructure tuples. It’s especially handy if a function returns 2 values in a tuple and you want to name each value individually.
Length
An array has some metadata associated with it, like its length. A tuple does not. Tuples are essentialy structs with anonymous field names.
Looping
Iterating over a tuple doesn’t work. For a tuple, each iteration could result in a different type, that doesn’t work. Since arrays can only hold a single type, they don’t have that limitation.
Ok, fine. I’ll iterate over a number range and access each item in the tuple via the dot synt…NOPE
Tuples can’t be dynamically indexed.
The compiler needs to be able to figure out what type every value is, indexing into it with a variable makes this impossible. If the compiler doesn’t know which index is being used, it can not figure out what type the value at that index is.
Here’s an (slightly contrived) example of why you can’t do stuff like that in Rust:
In this case, the compiler wouldn’t be able to figure out at compile-time what type y should be! Whereas with an array, you know (and the compiler knows) that indexing is always going to return the same type.