In this lecture, we are going to explore another type for storing a collection of data called vectors.
Typically, vectors are more popular than arrays.
They both serve the same purpose.
A vector can store a list of data.
The data type in a vector must be of the same type.
So what's the deal?
Why is there an alternative type?
Arrays have a fixed size if the size of an array is five.
We can't store a four or six values.
The size must exactly be five, whereas vectors are dynamically sized that are much more flexible than
arrays.
Pushing or removing data from a vector is allowed.
Vectors can be as large as we need them to be.
There are other differences, but this is the most important difference.
It seems like vectors are superior in every way.
However, vectors take up more storage.
Russ needs to allocate memory for a vector, which is not as efficient as arrays, while vectors are
more popular with Russ developers.
There are use cases for arrays.
For example, let's say you're creating a financial application.
Users may want to view the price of a stock over the course of a week.
The stock market is open five days a week.
This type of data can be stored in a vector or array.
However, arrays may be better suited for this task.
We only need to store five values.
Therefore, an array will suffice.
Arrays are great for smaller collections with a fixed size.
Vectors can be used for everything else.
Enough theory.
Let's create some vectors.
Russ gives us two options for creating a vector.
I don't consider either option superior over the other.
Feel free to use whichever syntax you prefer below the items array, let's recreate the same array.
The name of the variable will be called vector items.
The first option for creating a vector is with a macro.
We can set the variable to the vector macro.
This macro doesn't need to be called like a function we can immediately start creating the collection
with a pair of square brackets like before vectors are a list of comma separated values.
Let's input the same values of our array.
Feel free to copy them over.
Next, let's explore the second option for creating a factor.
We're going to recreate the same vector below the variable defining variable called vector items to.
The value for this variable will be a function called the AEC colon colon new.
We're utilizing a feature called Struts, which is short for structures, a structure similar to the
idea of an object or class.
They are containers for multiple pieces of data.
We aren't going to dive into structures for this course.
It's enough to know how to access a function from a structure.
We can access a function by typing to colons.
And this example, we are calling the new function to create a new vector.
By default, the vector will be empty.
We should start pushing values.
Before we do, let's add the immutable keyword to the variable.
Otherwise, we won't be able to push values into the vector.
New values can be inserted into the vector by calling the push function on the vector.
We can pass any value to push into the vector.
Let's push the same five numbers we had in the previous vector.
Creating vectors is a relatively simple process.
There isn't a difference between these two sin taxes.
Behind the scenes, the vector macro is calling the new function to create a vector afterward.
It's calling the push function to add new values.
You can think of it as sugar syntax for creating a vector.
At the end of the day, there isn't a difference in performance.
Choose whichever option feels right to you.
To test our code, we should print these vectors at the bottom of the function.
I'm going to quickly print both vectors.
Since we're dealing with factors, we will need to format the string using the debug placeholder.
Make sure you're typing the colon and question mark characters inside the curly brackets inside the
command line.
Let's compile the project.
Three collections are printed to the console.
The first collection is an array, while the other two collections are vectors.
They look very similar.
Internally, they are very different.
As we discussed before, they have their use cases.
If you need a fixed sized collection.
Use an array.
Otherwise stick with a vector.
Let's continue our discussion of rust in the next lecture.