Tuples in Rust

Pascal Precht
InstructorPascal Precht
Share this video with your friends

Social Share Links

Send Tweet
Published 5 years ago
Updated 4 years ago

Learn how to represent multiple values of different type using a single type by leveraging the power of tuples in Rust.

Instructor: [00:00] Tuples are collections of values that can have different types. To create a tuple, we write a sequence of values separated by commas, surrounded by parentheses.

[00:10] For example, we could create a variable point that represents a point in a coordinate system using a tuple like this. In this case, the tuple would be of type (i32, i32). However, it's totally possible for a tuple to have different types.

[00:29] Tuples can have more than two values as well. For example, we could say a point has an id, which in this case would be of type string. Then the tuple would become a triple of type (string, i32, i32).

[00:44] To access the values of a tuple, we would use constant indexes. In this case, we could say println!. Then we say Point placeholder, then placeholder, another_placeholder. Then we give it point.0and point.1 and point.2. When we save and run this program, we'll see it'll output Point A, 32, 34.

[01:09] Tuples are useful for cases where we want to represent multiple values of different type as a single type without necessarily introducing a new custom type. Just like any other type in Rust, a tuple can be returned from functions as well.

[01:24] For example, if we have a text of "Hello, world," we could call its split method on it, which would return a head and tail of the text. In this case, the returned value would be a tuple of type string slice reference and string slice reference.

[01:46] Tuples can hold up to 12 values. For consistency's sake, a tuple can hold a single value as well. We could have a variable a of type tuple, some_value. This would be totally valid. Notice, however, that there is a trailing comma here. This is needed in order to have Rust distinguish between this tuple expression from a simple expression such as 1 + 1.

J. Matthew
J. Matthew
~ 5 years ago

Another interesting type. This seems functionally similar to any[] in TypeScript, only with a maximum length. Is tuple the only option in Rust for an array-like of mixed types? Short of creating your own custom types, I mean.

Tuples can hold up to 12 values.

I'm confused about this. I tried creating a tuple bigger than that just to see what would happen:

let tuple = (1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14);
println!("14th value: {}", tuple.13);

It still works.

let (head, tail) = text.split_at(5);

I appreciate that Rust allows this kind of destructuring syntax, as opposed to requiring something like this:

let split = text.split_at(5);
let head = split.0;
let tail = split.1;
Pascal Precht
Pascal Prechtinstructor
~ 5 years ago

This seems functionally similar to any[] in TypeScript, only with a maximum length.

Not exactly, because any[] doesn't dictate the order of types in use while a tuple (e.g. (i32, &str, String)) does.

Is tuple the only option in Rust for an array-like of mixed types?

I think so. There are other collection types like HashMap<K, V>, where K and V can be of different type, this of course also has different semantics.

I'm confused about this. I tried creating a tuple bigger than that just to see what would happen:

To be honest, I trusted the number I've read in my book and never verified it myself. Very good catch! Will update this episode accordingly!!

Anthony Albertorio
Anthony Albertorio
~ 4 years ago

Say I update part of a tuple with new values. Must the type order remain the same?

Pascal Precht
Pascal Prechtinstructor
~ 4 years ago

Hey Anthony,

yes, the order has to remain the same otherwise you'd actually change the type of value. So for example:

let mut point = ("A", 8, 9); // (&str, i32, i32) <- this is the current type

point.0 = "B"; // <- this works fine as `point` is mutable and `point.0` is of type `&str`

point.0 = 3; // <- this will be a compilation error because the expected type of `point.0` is `&str` (per initialization)
Anthony Albertorio
Anthony Albertorio
~ 4 years ago

Makes sense! Thanks, Pascal!

Great courses so far! They are to the point and pack a lot of information. I appreciate your style.

I have learnt a lot!

Markdown supported.
Become a member to join the discussionEnroll Today