Another way to have a collection of multiple values is with an array.
Like a tuple.
Arrays are going to have a fixed length.
But an array is different from a tuple because every element of an array must have the same type.
Now there are a couple of different ways that we can set up an array.
With the first one being, let's create an array variable, give it some brackets, and then we're going
to create an array that holds integers.
So here we created our first array.
And assigned values one, two and three inside of it.
Now, like a tuple arrays are also going to start counting from zero when you are referencing their
index.
So index zero is going to contain the value one index one is going to contain the value two and so on
and so forth.
So if we wanted to access one of the values inside the array, we will say array, open and close bracket,
and then we will give the index that we want.
So what this is going to do is it's going to access the first element in the array force, which in
this case is one, and that's exactly what we got.
Another way that we can set up an array is saying, Hey, I want an array, but this time I want it
to be assigned integer 32 bits and then I want it to have a specified length of three.
And then in here you do the same thing.
So we'll give it another.
Three values, and in this case it is four, five and six, and it works the same exact way as before.
So now what happens if we want to modify a value inside of an array?
So if we came down here and said array two of index zero equals.
Ten.
That would be me trying to change the value four to the value ten.
So do you think this will work or will we get an air?
If you said you think we will get an error?
You are correct.
Because we did not make our array mutable.
And luckily, if you happen to make this mistake, the rust compiler is friendly enough to tell you
and it tells you, hey, you need to make your array mutable.
So now if we go up here, make it mutable, and then try it again.
We're good to go.
So we changed the first value in the ray from 4 to 10.
Now, another thing is, is what happens if we try to reference an index outside of the array?
Meaning, since we only have three values, the highest index we should technically be able to go is
to.
But what happens if we tried to reference index number three, which does not exist in this case?
So if we do that, what do you know?
The rust compiler is smart enough to tell us.
Hey.
Prior to runtime you.
Try to reference an index that.
Doesn't exist.
So it's already doing that.
Checking for us.
But what happens?
If we did it dynamically at runtime?
Well, it tells us right here this operation will panic at runtime.
And that is really good for us because we can actually handle panics in our code, which we will get
to later in this course.
And the big the big deal behind this is is it's going to.
Prevent the user and our program from being able to access.
Unauthorized memory.
So the rest compiler is really good and it's also very smart at being able to check our code prior to
runtime to hopefully help us catch bugs before we put them into production.