Check out a free preview of the full The Rust Programming Language course

The "Tuples" Lesson is part of the full, The Rust Programming Language course featured in this preview video. Here's what you'd learn in this lesson:

Richard discusses how to define a tuple in Rust, set the values in a tuple to a variable, mutate tuples, and explains what a unit tuple is. Values in a tuple do not have to be the same type and can be set individually or by using destructuring syntax.


Transcript from the "Tuples" Lesson

>> All right, part two collections. So in this next section, we're going to talk about tuples, structs, arrays and memory. Let's start with tuples. This is how I can define a tuple in rust. So basically I use these parentheses here. So you'll notice that I'm saying (0, 0, 0), that's making a tuple, which has three elements in it.

The title of the tuple is ( i64, i64, i64), in this case. Now, you can mix and match these as much as you want. They don't all have to be the same type, but they just happen to be in this case. And you can also have more than three of these or fewer than that if you want.

If I wanna read some value out of this tuple. I can say let x = point.0, and that's going to get the first element in there. We can also say y is point.1, Z is point.2. So you can imagine this might be a 3D coordinate point. And this would be how I would get the values out of there.

I can also use destructuring syntax as a way to get x, y and z into scope. So x, y and z equals point. This does exactly the same thing as this previous line. This is basically just syntax sugar for this. So x is going to be this one, y is going to be the middle one, and z is going to be the last one.

If you want, you can also D structure only some of them, not all of them. So you can do that by putting an underscore in place of any of the ones that you don't care. Underscore and rust generally means I don't want to bother naming this thing although I need to put something here.

So rather than picking a name and having it be unused, I'll just put an underscore here to say I only care about x and y from this particular point. If you want to have only x, you do that too and have multiple underscores. In fact, you can have as many underscores as you want, whenever you're doing destructuring.

If you want to mutate a tuple you need you do need to use let mut. So you may recall that we mentioned previously that let by default is both cost and immutable and let mut is neither. So this allows me to not only reassign something to point but also to mutate any of these individual values inside of it.

So now I can say point.0 = 17. To change that from a zero to a 17, I can change point.1 as well, and point.2. But again, if I didn't have let mut, none of these would compile. The rust compiler would say, Nope, you need to have a mut there in order to make it mutable.

Unit is a special tuple which is basically these zero tuple. So if we saw here we had a tuple with three values in it, you can also have tuples with two values. There's really no such thing as a tuple with one value because that's just like the thing itself and you can have more than three if you want.

But there's also a special tuple called unit which is basically a zero tuple. This is a tuple that has nothing inside of it. It's a tuple holding no information. It's extremely boring, because there's only one zero tuple ever and it's called unit. So you can never have more than one of these because they hold no information whatsoever.

And in fact, they're guaranteed to never hold any information. Because tuples cannot change their sizes at runtime. You can't have more elements in the tuple. There's no like, I want to push a new element into a tuple at runtime, can't do it. It's got a fixed number of slots that are always constant throughout the life of your programme.

And in this case, it's fixed at zero slots for the life of your program. So you might be asking the question, why is this in the language? What is the use of having a zero tuple, something that guaranteed has no information associated with it. Well, basically it comes up when you're writing functions that have no return value.

At least, that's one case where it can come up. So let's say I have my main function. We said earlier that main doesn't return anything, but actually this is really just syntax sugar for saying it returns tuple. So many times you have a function or a macro, which quote unquote returns nothing, in rust every function is actually required.

To have a return type, but unit is basically used as sort of the default. If you don't wanna specify one. It's basically saying, okay, yes, I have a return type II return type is unit which is to say I have nothing of interest to return. So, you can safely disregard whatever I'm returning.

So technically if you want to, you can even say let print line return Val, which has the type unit equals and then call print line. So When you're calling a macro, unless you have a list, it's on its own line with a semicolon at the end. This is still technically an expression and yeah, it'll return a value, but that value is going to be unit, so it's not going to be of any help to you.

So suddenly we just have the term, use the term void for something similar to this. Technically, yeah, you can take unit you can even pass a unit around, but in practice. This is how it's most commonly used as basically a rusts equivalent of what other languages use the term void to me

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now