- So far in the course,
we've been looking at simple types such as integers
and floats and Boolean and characters.
But Rust provides many different ways
to group data together into aggregate variables
which makes life much more interesting.
So for example, you can create an array or tuple.
An array is a fixed-size construct
where each element is the same type.
And a tuple is a fixed-size construct
where you can have different types of data.
So a tuple, for example, element zero might be a string,
element one might be an integer,
element two might be a float.
Okay, you can modify them potentially
if you declare them as mutable.
You can't change their size.
You can just change the values.
So as well as arrays and tuples,
we also have various different collection classes
such as vector and map.
A vector is a resizable collection, like a resizable array,
and a map is a dictionary with keys and values.
So first of all, arrays.
An array is a fixed-size homogeneous collection
where each element is the same type and it's fixed-size
so you can't extend it afterwards.
You can use square brackets syntax
to create a literal array, as I've said, shown here.
If you want the array to be mutable,
then just use the mut keyword
where you declare your array variable
that allows you to change the values inside the array.
Although, like I said, you can't change its size.
If you want resize ability, then you should use vector
and would have a look at that later.
So here's an example.
I've declared a variable a1.
It's an array of integers, an array of i32.
a2 is mutable, so I can change the values in a2.
I can't change the values in a1.
Right, to use an array, square brackets.
You specify the index.
The index must be within range.
So if you have an array of length 10, for example
first element is zero, last element is nine,
and Rust will basically give you an error
if you exceed the bounds.
If you use a literal index like index 11
and the compiler knows for a fact
that you've exceeded the limits,
then you'll get a compile time error.
If you have a variable index i at runtime,
bound checking will still take place.
Okay, and your application will terminate
if you exceed the bounds, so don't.
If you're not sure, then you get the length of the array
using the length function.
So technically, the array construct has a length method,
gives you the length of the array
and then you can use that in your error checking.
Right, most commonly with an array,
you will iterate over the elements.
You can use a for end loop like this.
So if you have an array called arr,
each element would be copied into the elem variable here
and then you can process the elements,
print them and modify them potentially
if the array is mutable.
So it's all kind of quite straightforward really,
pretty much what you might expect from other languages.
So we'll have a look at an example.
The project for this lesson
is lesson05_compounds_collections.
So it talks about compound data types
like arrays and tuples,
and it covers collections like vector and map.
And in that project,
I've got a bunch of different functions.
For this particular section,
the function we're gonna look at is called demo_arrays.
So we'll go through the code
and then we'll run the project as normal.
So let's have a look.
This is how it should look on your file system.
The Rust dev demos folder,
lesson five, as I said, this is a cargo project,
lesson05_compounds_corrections,
and let's open that up in the code editor.
So here we are, simple main file, main.rs,
and in here, I've imported the HashMap structure.
We'll have a look at that towards the end of this lesson.
We'll ignore that for now.
I've got various different functions
that we're gonna call from main,
same kind of arrangement as usual.
First function we're gonna call demo_arrays
and here it is.
So I'm gonna run the application first,
usual kind of arrangement
so that we kind of see the output.
Cargo run.
And at the bottom here in the arrays function Using Arrays,
here we are.
So I've declared a immutable array, a1,
containing those three values.
You can get the length of the array,
the length of the array is obviously three.
It starts indexing at zero, like most languages.
So the first element is element zero,
that's 100, that's correct.
All good.
Here's a mutable array.
So remember, you can change the values,
but you can't change the size.
So I'm gonna change element zero to be 999.
Let's get the length and the first element.
The length of a2 is three
and the first element is now 999.
That is correct.
And then we can iterate over the array,
so give me each element in a2, so that's my a2,
and print each element on the console.
So those are the three elements displayed.
Okay, so that's a quick introduction to arrays.
What we're gonna do in the next section is go deeper
and look at some array techniques.