1 00:00:06,110 --> 00:00:09,980 An additional method that we can add is Peking. 2 00:00:10,580 --> 00:00:16,250 Peking is a method that is going to allow us to get a reference to the element in the head of the list 3 00:00:16,250 --> 00:00:17,330 if it exists. 4 00:00:17,330 --> 00:00:21,410 But Peking does not remove the value like Pop does. 5 00:00:21,410 --> 00:00:28,220 So the key difference here between Peking and popping is Peking is going to give you a reference to 6 00:00:28,220 --> 00:00:28,940 the head. 7 00:00:29,120 --> 00:00:34,040 Popping is going to give you the value that was in the head. 8 00:00:34,460 --> 00:00:38,420 So let's go ahead and implement this in to see what it looks like. 9 00:00:38,990 --> 00:00:47,390 So we will say peak and this is going to refer to our self and we are going to return an option with 10 00:00:47,390 --> 00:00:51,290 a reference to the data inside of head. 11 00:00:51,680 --> 00:01:00,950 So we will say self dot head as a reference and now we want to map it. 12 00:01:02,740 --> 00:01:03,840 Say node. 13 00:01:07,630 --> 00:01:11,260 And now we just want to return our node dot. 14 00:01:12,940 --> 00:01:19,300 So dot as ref right here converts from. 15 00:01:20,970 --> 00:01:38,550 Let's write this out as ref converts from an option t to option and t so it's a reference to the internal 16 00:01:38,550 --> 00:01:40,440 value, which is what we want. 17 00:01:42,690 --> 00:01:47,280 Another form of peak that we can have is a mutable peak. 18 00:01:47,280 --> 00:01:50,640 So we can say peak mute. 19 00:01:51,030 --> 00:02:04,320 And this is going to be a mutable reference to our self and we will say option and mute T And as you 20 00:02:04,320 --> 00:02:09,000 can guess, this is probably going to be very similar to what we just had. 21 00:02:09,000 --> 00:02:17,370 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 22 00:02:17,370 --> 00:02:27,330 going to map it to our node and we want to return a mutable reference to node dot element. 23 00:02:27,900 --> 00:02:31,350 So let's do a cargo format to make this a little prettier. 24 00:02:31,380 --> 00:02:32,220 There we go. 25 00:02:33,300 --> 00:02:39,810 So now what we can do with this is we can come down here and we can extend our test cases. 26 00:02:40,050 --> 00:02:42,990 So we want to add a new test case called peak. 27 00:02:43,590 --> 00:02:46,590 And what we're going to do is we need to create a new list 28 00:02:49,470 --> 00:02:54,630 and now we want to verify that our peak is working correctly. 29 00:02:55,800 --> 00:03:04,020 So using the assert equals macro, Again, if we list peak while the list is empty, we expect none 30 00:03:04,020 --> 00:03:04,980 to be returned. 31 00:03:06,510 --> 00:03:10,620 We also expect that to be the case when we do list. 32 00:03:12,440 --> 00:03:14,060 Pick mute. 33 00:03:17,510 --> 00:03:24,290 Now that we know that those are tested, we can push in a couple of additional values. 34 00:03:29,870 --> 00:03:34,640 So we will say two, three, 35 00:03:38,030 --> 00:03:40,760 four and five. 36 00:03:42,920 --> 00:03:51,740 So now that we have some values in our list, we can start checking to make sure that our peak is actually 37 00:03:51,740 --> 00:03:53,180 returning what we want. 38 00:03:53,180 --> 00:04:00,050 So we will expect some and then we expect a reference to five to be returned. 39 00:04:02,190 --> 00:04:03,780 And let's do this again. 40 00:04:04,110 --> 00:04:07,110 But this time with peak mute. 41 00:04:07,770 --> 00:04:15,690 And notice that we're still referencing the same five value that will be in our list because peak does 42 00:04:15,690 --> 00:04:17,490 not remove the value. 43 00:04:17,610 --> 00:04:22,290 So that is also going to be a good test to make sure that our value is not being removed. 44 00:04:23,880 --> 00:04:27,690 So let's do another peak mutable. 45 00:04:27,900 --> 00:04:34,560 But this time we want to map we want to map this and we want to say. 46 00:04:37,830 --> 00:04:38,970 The value. 47 00:04:40,200 --> 00:04:42,240 Let's just do something simple with it. 48 00:04:42,600 --> 00:04:47,340 Like we want to change it to, let's say, 42. 49 00:04:49,800 --> 00:05:01,260 Well, now, if we execute these again, if we run peak, we expect 42 to be returned. 50 00:05:03,440 --> 00:05:07,790 And then let's make sure our pop works fine. 51 00:05:08,240 --> 00:05:12,500 And now we would expect a regular 42 to be returned. 52 00:05:12,830 --> 00:05:18,380 So let's run our test cases and we see that we have an issue here. 53 00:05:18,920 --> 00:05:21,380 Types defer and mutability. 54 00:05:21,380 --> 00:05:23,600 And this is on line 97. 55 00:05:23,600 --> 00:05:28,400 So let's see if it is wanting it to be like that. 56 00:05:28,880 --> 00:05:37,610 So our test case on the peak mute, we need to make sure that we specify that it is a mutable reference. 57 00:05:38,690 --> 00:05:47,750 And once we have done that, we see that our test cases and I need to make sure our test runs, I need 58 00:05:47,750 --> 00:05:52,420 to put that their cargo test. 59 00:05:52,430 --> 00:06:02,840 We see that our peak test also ran successfully, so we successfully implemented in peak and peak mute 60 00:06:03,320 --> 00:06:06,020 onto our linked list. 61 00:06:06,380 --> 00:06:14,540 So now in the next lecture we are going to continue diving further into what methods we can implement 62 00:06:14,540 --> 00:06:16,640 in on a linked list.