Vectors are a resizable array of elements allocated on the heap.
Vectors are an essential type and rust because they are used almost anywhere.
One needs a list of dynamic size.
The easiest way is to use the VEC macro and as a reminder, we will talk more about macros later on
in this course.
But for now let's just look at how to create a vector using the VEC macro.
So I went ahead and created a new variable called nums and we are going to use the VEC macro to create
a vector.
So I'm going to put in the values one, two and three.
And now remember I said that vectors are resizable, so let's add a value into this vector already and
call it and give it the value four.
So now let's print this out and see how it works.
Now this is going to produce an error, but I want you to see the error so that we can understand why
it's an error.
So it says vec of type integer cannot be formatted with the default formatter.
So if we continue reading down and this is just showing you how helpful the rust compiler is, it will
say you may be able to use a colon and a question mark.
Instead.
So basically what that is, is that's going to allow us to put it into debug mode.
That's what the question mark is.
And the the colon basically says, take the next thing and then push it into the formatter and output
it into debug mode.
So now that we have that in there and we cargo build, don't forget to make your variable mutable since
we are changing it by adding it another number.
And now we can see we have the values one, two, three and four printed out.
But we can also remove values, so we can use the pop method that a vector provides us.
And what pop is going to do is it's going to take the last value that we pushed into the vector and
remove it.
So let's pop one and then let's print it out.
And it should remove the four from the vector, which is exactly what it did, which is awesome.
So there is another way that we can create vectors.
So let's go ahead and do that.
And it's called Vec Nu.
So when we use the macro, this is essentially.
What it's calling is the new constructor.
So now that we created a new empty vector, again, we can push values onto here.
So let's call it test and then let's push on.
String.
Let's print it out.
Just to make sure that we are working correctly.
Clear it.
Build it.
Run it.
And we have test string.
So now what's really cool about vectors, they come with a ton of methods that we can already use.
So I highly encourage you to explore all these methods after this lecture, just to really familiarize
yourself with everything available in the vector class.
So what we're going to do, just for an example, is reverse.
Vector that we created.
So by doing this, we should see when we print it out.
String test.
Which is exactly what we got.
We have string test.
So again, I highly encourage you that after this lecture just play around with all the different methods
that a vector provides and just really get familiar with them as they will help you in your rest career.
So another really cool thing that we can do is if you know the size that your vector is going to be,
we can actually specify that.
So let's start off with a new vector and we'll call this one vector and we're going to say Vec.
Now we have to give it a type since we are creating it empty in this case.
So I'm going to give it integer 32 with capacity two and then I would just want to print out just to
make sure that it created.
That capacity effect dot.
Book passage.
Here we go.
So let's build it.
We have an issue here.
It says, use fully qualified syntax.
So we said, let me vet.
Vet of type I.
32.
With capacity to.
Let's see if we can use the compiler to figure this issue out.
So I know that this one's not going to work.
And it's because we needed.
Greater than and less than around it.
So now that we use the compiler to figure it out, let's go ahead and run it.
So down here it says to which is what we set our capacity to be at.
So you might be wondering, well, if I create a vector with capacity to what happens if I end up needing
extra capacity?
Luckily the vector can grow if it needs to, and the way it works is it's going to allocate a larger
buffer, copy the present contents into the new buffer, and then it's going to update our vector variable
to point to the new buffer, and then it's going to free the old vector from memory.
So basically it's just going to get a new region of memory, push everything into it, and then delete
the old.
So that way it's very seamless for us to be able to expand it as we need to.
And then one other trick I want to show you is you can actually create a vector.
Using an iterator and we will talk about iterations later on in this course.
So we'll say we want to create a vector from value 0 to 5, not including five.
And let's go ahead and print it out.
And it should give us values zero, one, two, three and four, which is exactly what we got down here.
We have values zero, one, two, three and four.
So again, I highly encourage you to play around with all the methods available to vectors to really
familiarize yourself with them.
It is a very essential type in rust and it will immensely help you as you continue to learn rust.
And the next lecture, we will talk about what slices are.