- In the last couple of sessions,
we've seen how to declare an array,
and we saw that an array is a fixed size collection,
where each element is the same type.
So what we're going to do now is look at tuples,
kind of similar,
but where each element can be different types.
So a tuple is fixed size, heterogeneous collection.
The elements can be of different types.
Instead of using square brackets to create like we would
for an array, we use parentheses to create a tuple.
So this here is a tuple
with three elements.
Start at element zero.
That's element zero, that's element one,
and that's element two.
Okay, so parentheses instead
of square brackets for a tuple.
If you want to, of course you can declare a tuple
as a multiple tuple, in which case you can change the values
but you can't change the types,
and you can't change the size either.
Okay? So if you replace one of the elements,
you must replace it with the correct type
and you can't resize it to be bigger.
So here's an example of a tuple.
T one is a tuple.
The parentheses indicate that.
The first element is an integer.
The second element is a string of some kind.
The third element is a floating point value.
T1 is immutable, T2 is mutable.
And T2 contains, well initially the same values
but they could change later on.
The main use for tuples really is to declare
or to return more than one value from a function.
So what you could do, let's say you want to return a bunch
of values from a function, you could put them
into a structure, but that's a bit of extra work.
You'd have to declare the structure type,
and maybe you can't be bothered to do that.
So instead, you can just throw the values into a tuple.
A tuple is like a bag,
and then return that tuple back to the caller.
That'd be the main use of tuples in practice.
So if you want to access an element in the tuple,
let's say you've got a tuple object called tup,
you've gotta use a dot and an index syntax,
you can't use square brackets like you would with an array.
You can't say tuple square bracket zero.
You've gotta say tuple dot, and then the index
of the element you're after,
like tuple.zero.
So rust does bounds-checking at compile time, right?
Compile time.
It'll check that you said tuple.index is the
index between zero and length minus one.
And if it isn't, then you get a compile it error.
Okay? So obviously you have to be careful.
Things that you can't do with a tuple,
its not as flexible as an array.
Okay? So with an array
you can get its length, but you can't, with a tuple,
you can't say tuple object.length.
You can't do that.
And also you can't iterate
through the elements in the tuple, okay?
Because the main reason is the elements could be
different data types.
They probably will be.
So if the first element's an integer,
and the second is a strain,
and the third is a double, how does this construct work?
What type would this element be?
It just doesn't work.
So you can only access elements one by one
using an index number with a dot.
So it's much more kind of fixed,
less dynamic if you like, than an array.
It allows you to have multiple data types.
That's the key point.
So, well here you can have an empty tuple.
The empty parenthesis, it's a tuple with no elements, okay?
So it would be useful
for a function that doesn't want to return anything.
You could return an empty tuple.
That would be the kind of use case for an empty tuple,
a return value that's empty from a function.
So if you want to declare a tuple variable
but you're not yet ready to give it the values,
then what you can do is you can, you can declare a tuple
and then use the parentheses to specify the type
of element zero, the type of element one,
and the type of element two, and so on and so on.
So when you're then ready you can assign your tuple object
with values of your choice.
Okay? Given that the value
of the correct type for each position.
So here's an example.
I've got a tuple T4,.
it's a tuple by virtue of the parentheses,
where element zero, I haven't given it the values yet,
but element zero will be a 32 bit float.
Element one will be a Boolean, and element two will be
a 64 bit float.
So later on when I'm ready, I can assign it a 32
bit integer, my age.
I can assign it the Boolean value
is this person Welsh?
And I can assign, 'cause that's important.
And then I can assign a floating point value my height
okay? As long as the elements of the same type,
then that's okay.
So specify the types upfront, give the values later
when you're ready, right?
So rust tuples implement debug just like arrays.
In other words, the tuple data type has a format function
which basically displays the elements of the tuple
in a user-friendly format.
Okay? So you can use the debug formatter like this
with the tuple curly bracket, colon, question mark.
And then here's my tuple object.
So effectively what will happen is it'll call the format
function on the tuple.
And that's pre-written, that's already defined in rust.
The format function will output all the elements
that the tuple contains.
So an example, let's go to the demo project.
We'll have a look at demo tuple as a function
and then we'll run the project like usual.
Let's have a look at the code.
So here's my demo project, compound collections,
in my source main.
I'm going to un comment the call to demo tuples.
Let's un comment that and let's take a look at it,
down here.
And there it is in all its glory.
We'll run it so we can discuss it.
All right, here we go.
Using tuples.
A tuple with those values.
That's an immutable tuple.
And I can access the elements one by one.
Tuple object.zero, tuple object.one, tuple object.two.
If I give it a value, an index out of range compiler error.
Okay? So it outputs the tuple elements like so.
Here's a mutable tuple, which I then change one
of the values of, I change element zero
and then I output the elements of tuple two.
And here are the elements in tuple two.
That's marvelous.
And here I have an empty tuple, which I output.
And here's my empty tuple.
It has no elements.
And then finally, a tuple.
I specify the types here, I give the values later.
And then in this example,
I'm using a debug formatter to output the whole tuple all
at once.
And that's displayed here.
And then if I want to,
I can then access the elements individually
one by one using the individual indexes.
Tuple four, element zero, tuple four element one,
and tuple four element two.
Fantastic.