1 00:00:06,480 --> 00:00:10,470 This lecture, we will begin implementing integrators on our list. 2 00:00:10,620 --> 00:00:18,510 The first one that we are going to implement is the end to ITER, and by implementing the into iterator 3 00:00:18,510 --> 00:00:22,530 we will define how list will be converted into an iterator. 4 00:00:22,770 --> 00:00:28,080 And this is common practice for types which describe a collection of some kind. 5 00:00:28,800 --> 00:00:35,220 So let's go ahead and begin setting up the implementation for our end to enter. 6 00:00:35,550 --> 00:00:41,640 The first thing that we're going to need to do is create a new struct called into editor over type T 7 00:00:42,270 --> 00:00:47,250 that is going to have a list that is of type T. 8 00:00:48,890 --> 00:00:56,960 So now inside of our list implementation block, we can come down to the bottom of it and we can say 9 00:00:56,960 --> 00:01:13,610 pub fin into ed on self and this is going to return an end to ITER where we say into ITER on self. 10 00:01:13,610 --> 00:01:21,650 So not much that we have to do there for this, but now we need to implement the iterator trait for 11 00:01:21,650 --> 00:01:35,000 into ITER so we can say simple iterator for into iter on type T and now this is where all the magic 12 00:01:35,000 --> 00:01:37,550 is going to happen for the actual iterator itself. 13 00:01:37,880 --> 00:01:47,150 So we will say type item is equal to T, and then we will implement in our next method. 14 00:01:47,600 --> 00:02:00,590 And next is going to be mutable reference to the self where we return an option on our self item. 15 00:02:00,590 --> 00:02:03,350 So where we return our item here. 16 00:02:06,650 --> 00:02:11,060 So in here, this is actually going to be something that's really easy for us to do. 17 00:02:11,270 --> 00:02:17,060 So if you look at how we set up our struck here, we set it up a little differently than here. 18 00:02:17,060 --> 00:02:24,800 So this is actually a tuple struct, if you remember that from earlier on in this course. 19 00:02:25,070 --> 00:02:32,150 So in order for us to reference our tuple struck, we can the first element, which is also our only 20 00:02:32,150 --> 00:02:38,900 element, we can say self dot pop or self .0. pop. 21 00:02:41,580 --> 00:02:47,100 And that's all we need to do in order for us to get the value into our iterator. 22 00:02:47,580 --> 00:02:49,650 So we have one iterator set up. 23 00:02:49,650 --> 00:02:54,090 There's another iterator that we can set up, call it. 24 00:02:55,260 --> 00:02:57,840 So let's set up our struct for that one. 25 00:02:57,840 --> 00:03:00,690 And this is going to look very similar to what we just did. 26 00:03:00,990 --> 00:03:05,640 So it's going to be ITER, and this time we're going to need to add a lifetime. 27 00:03:06,750 --> 00:03:15,990 And then also on our PT and the ITER is going to be generic over some lifetime. 28 00:03:15,990 --> 00:03:16,950 It doesn't care. 29 00:03:17,640 --> 00:03:23,850 And then we can use a struct, a normal stroke in this case to hold the reference. 30 00:03:23,850 --> 00:03:26,040 So we're going to need to hold a reference. 31 00:03:26,310 --> 00:03:29,970 So next and we're going to say option. 32 00:03:30,180 --> 00:03:34,770 And since it's going to be a reference, we need to specify the lifetime. 33 00:03:34,770 --> 00:03:38,910 So that is why we have a lifetime attached to this struct. 34 00:03:38,910 --> 00:03:44,850 So when you have a reference in your struct, remember you need to specify the lifetime of that reference. 35 00:03:46,470 --> 00:03:51,210 And now we have our iter struct set up. 36 00:03:52,950 --> 00:03:55,110 So now we do the same thing again. 37 00:03:55,110 --> 00:04:03,630 We come down below our into iter and now we're going to set up our iter, which is going to again be 38 00:04:03,630 --> 00:04:14,760 a reference to ourself and again we're going to return an error DT which is going to return in ITER 39 00:04:14,760 --> 00:04:15,990 struct. 40 00:04:17,400 --> 00:04:30,450 Where we say next self head as dx ref because we want a dx reference the box before we take our reference. 41 00:04:30,450 --> 00:04:32,610 That way we can get the value inside of it. 42 00:04:34,080 --> 00:04:41,220 So now we want to implement in the iterator 43 00:04:43,830 --> 00:04:49,740 trait for our ITR type. 44 00:04:50,070 --> 00:04:53,700 And remember we need to specify our lifetimes in all these. 45 00:04:56,550 --> 00:04:59,730 So now we have that set up. 46 00:04:59,730 --> 00:05:05,850 We can now begin implementing in the logic for our enter type. 47 00:05:07,050 --> 00:05:09,430 So it's going to look very similar to what we already did. 48 00:05:09,450 --> 00:05:16,920 So we'll say type item is equal to and again, since it is a reference, we need to include the lifetime 49 00:05:16,920 --> 00:05:17,820 in this case. 50 00:05:19,140 --> 00:05:19,980 And now. 51 00:05:21,480 --> 00:05:25,980 What we can do here is going to be, again, very similar to what we did above. 52 00:05:26,010 --> 00:05:27,780 So we'll say reference self 53 00:05:30,270 --> 00:05:35,370 where we return an option to our item. 54 00:05:41,380 --> 00:05:42,490 And now we say self. 55 00:05:42,790 --> 00:05:44,110 Next take. 56 00:05:45,380 --> 00:05:49,550 Now we want to map it on our node. 57 00:05:53,250 --> 00:05:55,200 And inside of here we will say self. 58 00:05:55,440 --> 00:06:02,450 Next equals node, next dot as dx ref. 59 00:06:03,600 --> 00:06:04,980 And then we return. 60 00:06:06,860 --> 00:06:10,280 A reference to our node element. 61 00:06:12,390 --> 00:06:18,180 So a lot of this probably looks really similar to some code that we have done above, which you can 62 00:06:18,180 --> 00:06:23,790 see here is equivalent to what we did here for Pop. 63 00:06:24,000 --> 00:06:28,580 So a lot of this is already been pretty much done for us. 64 00:06:28,590 --> 00:06:32,100 So implementing this in actually wasn't too bad for us. 65 00:06:32,280 --> 00:06:39,030 So what we need to do now is add in our test cases to make sure that our iterations work as we would 66 00:06:39,030 --> 00:06:39,930 like for them to. 67 00:06:40,590 --> 00:06:42,510 So we need to do our test. 68 00:06:45,120 --> 00:06:49,260 And so in here we will test our our integrators out. 69 00:06:52,320 --> 00:06:54,690 So we just say let mute list 70 00:06:57,780 --> 00:06:59,310 is equal to a new list. 71 00:07:00,690 --> 00:07:02,760 And now we will push in some values 72 00:07:11,550 --> 00:07:13,740 and now we will create our iterator. 73 00:07:13,740 --> 00:07:27,090 So let mute itor equal to list dot editor and now we will assert equals on Twitter dot next and we expect 74 00:07:27,090 --> 00:07:32,730 the value of some with a reference to three to be printed out. 75 00:07:34,770 --> 00:07:43,080 Because we're take we're starting at the head and if we iterate again we expect two to be and now if 76 00:07:43,080 --> 00:07:46,260 we do it one more time, we expect one. 77 00:07:46,800 --> 00:07:54,570 And then lastly, we would anticipate none being printed out or being returned. 78 00:07:54,570 --> 00:07:55,380 Excuse me. 79 00:07:56,580 --> 00:08:00,510 So let's run cargo test and see what we have. 80 00:08:00,630 --> 00:08:06,450 So it says we cannot it helps if I add the semicolon again. 81 00:08:07,770 --> 00:08:13,560 And if we run this, we see that our iterator test passed successfully. 82 00:08:14,940 --> 00:08:18,930 So we have created in two. 83 00:08:20,030 --> 00:08:20,680 The Raiders. 84 00:08:20,680 --> 00:08:27,880 We did our ED and we also did our into hitter, which are two different types that are implemented implementing 85 00:08:27,880 --> 00:08:30,010 in the iterator trait. 86 00:08:30,790 --> 00:08:39,310 So in the next lecture we will go over the assignment for the editor Mute iterator.