An additional method that we can add is Peking.
Peking is a method that is going to allow us to get a reference to the element in the head of the list
if it exists.
But Peking does not remove the value like Pop does.
So the key difference here between Peking and popping is Peking is going to give you a reference to
the head.
Popping is going to give you the value that was in the head.
So let's go ahead and implement this in to see what it looks like.
So we will say peak and this is going to refer to our self and we are going to return an option with
a reference to the data inside of head.
So we will say self dot head as a reference and now we want to map it.
Say node.
And now we just want to return our node dot.
So dot as ref right here converts from.
Let's write this out as ref converts from an option t to option and t so it's a reference to the internal
value, which is what we want.
Another form of peak that we can have is a mutable peak.
So we can say peak mute.
And this is going to be a mutable reference to our self and we will say option and mute T And as you
can guess, this is probably going to be very similar to what we just had.
But instead of as ref, we will say as mute and then the rest is going to be the same where we say we're
going to map it to our node and we want to return a mutable reference to node dot element.
So let's do a cargo format to make this a little prettier.
There we go.
So now what we can do with this is we can come down here and we can extend our test cases.
So we want to add a new test case called peak.
And what we're going to do is we need to create a new list
and now we want to verify that our peak is working correctly.
So using the assert equals macro, Again, if we list peak while the list is empty, we expect none
to be returned.
We also expect that to be the case when we do list.
Pick mute.
Now that we know that those are tested, we can push in a couple of additional values.
So we will say two, three,
four and five.
So now that we have some values in our list, we can start checking to make sure that our peak is actually
returning what we want.
So we will expect some and then we expect a reference to five to be returned.
And let's do this again.
But this time with peak mute.
And notice that we're still referencing the same five value that will be in our list because peak does
not remove the value.
So that is also going to be a good test to make sure that our value is not being removed.
So let's do another peak mutable.
But this time we want to map we want to map this and we want to say.
The value.
Let's just do something simple with it.
Like we want to change it to, let's say, 42.
Well, now, if we execute these again, if we run peak, we expect 42 to be returned.
And then let's make sure our pop works fine.
And now we would expect a regular 42 to be returned.
So let's run our test cases and we see that we have an issue here.
Types defer and mutability.
And this is on line 97.
So let's see if it is wanting it to be like that.
So our test case on the peak mute, we need to make sure that we specify that it is a mutable reference.
And once we have done that, we see that our test cases and I need to make sure our test runs, I need
to put that their cargo test.
We see that our peak test also ran successfully, so we successfully implemented in peak and peak mute
onto our linked list.
So now in the next lecture we are going to continue diving further into what methods we can implement
in on a linked list.