So let's start off by looking at vectors and look at some of the methods that are associated with the
vector.
So we'll create a mutable vector called nums, and we're going to just put in values of AI 32 and we
want our vector to be empty to start.
So we looked, we saw this earlier in the course.
We can use a push method to add values into the vector.
So let's do that and let's just add in a few values here.
So one way that we can remove values off the top is by using a method called pop, and this actually
returns a value.
So we can actually say let pop equals number pop, and then we can print out this value.
It's not implement.
So there we go.
So now we can run this.
We can see that we returned some three.
So this returns an option of type T.
So remember this can either return none or some T, and in this case it popped some three.
So think of this as a stack of plates.
So we're going to.
Push plates onto the top.
And then when we pop, we want to take off the last plate.
So in this case, three that we pushed on to the stack.
So we can also, just as a review, reference some of the numbers so we can say let two equals num one.
And since we're doing I 32, this would just be a simple copy.
But if we were doing another value that doesn't implement copy, we could create a reference to it in
this case.
So creates a reference if copy is not available.
And then just to show you that we were able to successfully copy it, we'll say too and we'll run it.
And we got two.
So great.
So.
There's a couple of other ways that we're able to return values.
One of them being let one equals numb dot first.
And as you can imagine, this is going to return to us the first value and our vector.
But again, this is going to return an option T, so it's going to be none if the vector is empty
or some t.
If.
Now if the vector is not empty because we're going to take out the first index.
So we want to return the we want to get the first value.
So let's make sure that one successfully did just that for so one.
And we returned some one, which is great.
That's the expected value that we were anticipating.
So we can also do that last and just like first, instead of it being first, this is going to return
the last value in the vector.
But we also have to first mute and last mute.
And as the name suggests, it's going to be the same as first and last, but it's going to borrow mutable
references.
Will borrow mutable references.
So if we want to check out the length of our vector, we can do nums, dot length, line short for length.
And then if we want to do a quick check to just see if our vector is empty, we can do nums dot is empty
and this is going to return a boolean, whereas this will actually return a value of the length.
Another way that we can add values inside a vector is using insert, but insert is going to allow us
to.
Insert at the index of our choice so we can say insert at.
Index zero.
So the first index and we want to insert ten, but we can also say nums dot insert at the three and
we'll put in 12 and then we can do nums dot insert two.
And we can put in the value of 25.
So like insert we can remove with nums dot remove and then we can just specify the index that we want
to remove.
So in this case, let's remove index three.
And then if we want to sort everything out, we can do nums dot sort.
And now let's look to see how it would appear sorted just to make sure everything looks good.
And now we're going to do nums.
So we run it.
We see we have 110, 12 and 25.
Looks sordid to me.
And we can also run a method such as reverse, which is going to do what we expect it to do, which
is just going to reverse it.
So in this case, since we've already sorted it, I expect it to be 25, 12, ten and one, which is
great.
That's exactly what we got.
So something else that we can do is we can actually have our vector shuffled.
So in order for us to do this, we need to go into our cargo file and we need to specify that we want
to bring in the RAND package.
So we're going to say Rand and then we're going to do version 0.8.4, which is the latest version of
Rand at the time of this recording.
We'll save that file.
And then we'll come to the top outside of Maine and we're going to say, use rand.
Seek and slice random.
And then we're going to say use rand.
Thread.
Rand.
And now down here, we can say nums, dot shuffle.
And it's going to say want a mutable reference to the value generated to us by thread random number
generator.
That is what RNG stands for.
And now if we print this out, it's going to shuffle our vector for us and we'll run this a couple of
times to make sure that it is being shuffled.
And we got ten, 25, one and 12.
And then we got 12 one, 25 and ten, and then we got ten, 12, 25 and one.
So it does appear to be shuffling as we intended.
So these are just some examples, examples of what vectors have to offer in rust.
There are many more methods we can use, so I do encourage you to play around with all the methods associated
with vectors.
That way you become familiar with all the.
Methods we have at our disposal.