1 00:00:06,050 --> 00:00:11,060 We have already used it some in previous lectures, but just to refresh. 2 00:00:11,180 --> 00:00:13,070 Let's see one really quickly. 3 00:00:13,070 --> 00:00:23,210 So we'll create a vector and we'll use the macro and we'll just say one, two and three. 4 00:00:23,360 --> 00:00:26,030 And it helps if I use the correct brackets. 5 00:00:27,720 --> 00:00:33,060 And then we can say for Val and Vic dot ed. 6 00:00:35,190 --> 00:00:42,390 Print line and then print the vowel that is in the iterator. 7 00:00:42,390 --> 00:00:47,550 So we'll say cargo run and we see one, two, three being printed out. 8 00:00:48,300 --> 00:00:57,270 So all integrators are going to implement a trait named Iterator which is defined in the standard library. 9 00:00:57,300 --> 00:00:59,280 The trait looks like this. 10 00:00:59,280 --> 00:01:01,860 So we have trait iterator. 11 00:01:05,860 --> 00:01:12,730 And then we're going to have type item and then we have effin next. 12 00:01:14,880 --> 00:01:26,490 Which is going to be a reference to the mutable self and it's going to return an option of self item. 13 00:01:31,110 --> 00:01:40,170 And then there's many default methods available to us once we implement next. 14 00:01:41,910 --> 00:01:54,660 So item is the type of value the iterator produces and the next method either returns a sum with a value 15 00:01:54,690 --> 00:02:02,010 contained in it, or it's going to return none to indicate the end of the sequence. 16 00:02:02,700 --> 00:02:06,840 And then we also have the end to iterator trait. 17 00:02:07,200 --> 00:02:15,060 And any type that implements in to iterator is an iterator because it's something we can iterate over 18 00:02:15,060 --> 00:02:18,270 by calling the end to enter method. 19 00:02:18,360 --> 00:02:23,310 So let's look at an example of this and I'm just going to comment that out. 20 00:02:24,590 --> 00:02:26,240 And we'll comment this out as well. 21 00:02:28,250 --> 00:02:36,890 So we'll create another VEC called VEC two and then here we'll do the same thing one, two and three. 22 00:02:38,390 --> 00:02:48,140 And then we'll say let mute editor equals a reference to our VEC and then we'll say into. 23 00:02:51,380 --> 00:02:52,070 Bitter. 24 00:02:53,720 --> 00:02:55,520 And then while. 25 00:02:55,730 --> 00:03:08,720 Let some V equal it to next, which is the method given to us here because we created an iterator this 26 00:03:08,720 --> 00:03:09,170 way. 27 00:03:10,220 --> 00:03:24,710 By calling the intuitive method, we can now print out the values inside of some V, and if we run this, 28 00:03:24,710 --> 00:03:29,990 we expect the same output of one, two and three grade, which is exactly what we got. 29 00:03:30,770 --> 00:03:42,650 So just to clarify, an iterator is any type that implements the iterator trait and and iterable is 30 00:03:42,650 --> 00:03:46,310 any type that implements into iterator. 31 00:03:47,910 --> 00:03:49,920 Now remember last lecture? 32 00:03:49,920 --> 00:03:53,310 I said closure sign when using iterator. 33 00:03:53,580 --> 00:03:56,790 So let me expand on that. 34 00:03:57,060 --> 00:04:00,300 So we will comment all of this out. 35 00:04:00,480 --> 00:04:04,380 And up here, I'm also going to just go ahead and comment. 36 00:04:05,220 --> 00:04:06,390 All of this out. 37 00:04:07,910 --> 00:04:08,990 As well as that. 38 00:04:09,960 --> 00:04:11,670 And as well as. 39 00:04:13,630 --> 00:04:14,380 These. 40 00:04:15,860 --> 00:04:18,800 So I want to create a new struct called item. 41 00:04:20,000 --> 00:04:22,760 And in here I want it to be. 42 00:04:25,040 --> 00:04:26,780 String that has a name. 43 00:04:26,780 --> 00:04:32,120 And we're also going to give us the drive debug trait here as well. 44 00:04:32,990 --> 00:04:39,140 So now we're going to create a function that's going to be called check inventory. 45 00:04:40,690 --> 00:04:46,570 And we're going to accept a vector of items. 46 00:04:48,100 --> 00:04:53,200 And then we want to check to see if a product exists by accepting an A string. 47 00:04:54,810 --> 00:04:58,560 And then we'll return back a vector of item. 48 00:05:00,060 --> 00:05:04,800 And then in here, we want it to be items into. 49 00:05:05,720 --> 00:05:10,970 Ed so create an iterator and then we want to filter. 50 00:05:10,970 --> 00:05:13,400 So now we have another iterator. 51 00:05:13,400 --> 00:05:17,090 So as we see here, it creates an iterator for us. 52 00:05:17,210 --> 00:05:22,970 And this is where our closure is going to really help us. 53 00:05:22,970 --> 00:05:31,730 So we'll accept an a parameter and then we want to check to make sure that name equals a product and 54 00:05:31,730 --> 00:05:36,470 then to return it back to us in a vector we want to collect on this. 55 00:05:38,820 --> 00:05:52,440 So in here we'll create a vector of type vector item, which is going to just be a new vector. 56 00:05:53,760 --> 00:05:57,660 And so now we'll push in some values. 57 00:05:57,660 --> 00:05:59,250 So we'll push in an item. 58 00:06:00,300 --> 00:06:08,040 And then in here our name is going to be a string from and we'll have a coat. 59 00:06:10,280 --> 00:06:11,300 In this case. 60 00:06:11,720 --> 00:06:15,890 So now let's push in a couple more additional items. 61 00:06:17,300 --> 00:06:23,180 We'll have a and we'll add in our semicolons. 62 00:06:23,180 --> 00:06:25,070 And I will add have a shirt. 63 00:06:27,180 --> 00:06:32,840 We'll have some shorts and we'll have some shoes. 64 00:06:32,850 --> 00:06:39,600 So, again, a very simple little example for us to kind of understand what's going on. 65 00:06:39,690 --> 00:06:44,520 So then we'll say get checked equals check inventory. 66 00:06:44,850 --> 00:06:56,130 We'll pass in our VEC and then we want to make we want to see if we have a string from shirt. 67 00:06:56,130 --> 00:07:04,020 So we want to see if we have a shirt in stock in our inventory and then we will print out. 68 00:07:07,520 --> 00:07:08,930 What is returned to us. 69 00:07:10,500 --> 00:07:13,530 So let's run this and just see what happens. 70 00:07:14,130 --> 00:07:19,770 So we see that we have an item with the name shirt return to us. 71 00:07:20,190 --> 00:07:23,070 So let's go back up here and kind of look at what we did. 72 00:07:24,030 --> 00:07:31,020 So we use an end to iterate, to create an iterator that's going to take ownership of the vector, and 73 00:07:31,020 --> 00:07:39,930 then we call filter to adapt that iterator into a new iterator that only contains elements for which 74 00:07:39,930 --> 00:07:41,940 the closure returns. 75 00:07:41,940 --> 00:07:42,660 True. 76 00:07:43,260 --> 00:07:49,440 The closure captures the product parameter from the environment, and it's going to compare the value 77 00:07:49,440 --> 00:07:55,560 with each item we created and only keeps the item we specified, which in this case was a shirt. 78 00:07:55,890 --> 00:07:58,320 So it's only going to keep the item. 79 00:07:59,240 --> 00:08:00,290 There's a shirt. 80 00:08:01,410 --> 00:08:08,880 And then we call collect to gather the values returned by the adapted iterator into a vector that will 81 00:08:08,880 --> 00:08:10,770 be returned from the function. 82 00:08:11,400 --> 00:08:19,230 So we used a closure in this case to quickly evaluate logic without having to do a separate function 83 00:08:19,230 --> 00:08:24,060 call, which allows us to keep our code clear and concise. 84 00:08:25,000 --> 00:08:29,920 So that is one of the ways that a closure really shines. 85 00:08:29,920 --> 00:08:40,390 When we use iterator, we're able to quickly and efficiently have short and concise logic in order to 86 00:08:41,050 --> 00:08:42,130 evaluate. 87 00:08:42,830 --> 00:08:45,080 Values coming into our closure. 88 00:08:46,350 --> 00:08:49,890 But now what if we wanted to create our own closure? 89 00:08:50,010 --> 00:08:50,640 Excuse me? 90 00:08:50,640 --> 00:08:52,020 Our own iterator. 91 00:08:52,260 --> 00:08:54,810 So we'll create another struc. 92 00:08:57,360 --> 00:09:06,300 And we'll call this one arrange and we'll give it a start and then it'll be unsigned 32 and then we'll 93 00:09:06,300 --> 00:09:10,590 give it an end of unsigned 32 as well. 94 00:09:12,250 --> 00:09:24,160 And now remember, we have to implement an iterator for range and now we need to implement the trait. 95 00:09:24,160 --> 00:09:33,190 So we're going to have type item and it's going to be unsigned 32 and now we need to implement next. 96 00:09:34,120 --> 00:09:47,680 So we'll have a mutable reference to self and it's going to return an option of self item. 97 00:09:48,190 --> 00:09:49,180 Is that how it was? 98 00:09:51,750 --> 00:09:52,800 So self item. 99 00:09:55,990 --> 00:10:08,560 And here we will say if self start is greater than or equal to self end, then we want to return none. 100 00:10:10,890 --> 00:10:15,900 Otherwise let result equal some self. 101 00:10:15,900 --> 00:10:17,310 Don't start. 102 00:10:19,660 --> 00:10:28,780 And then sell, start, increment it by one and then give us the result. 103 00:10:30,320 --> 00:10:34,160 So to check to make sure that this works for us. 104 00:10:36,900 --> 00:10:39,180 Let's create a range 105 00:10:42,090 --> 00:10:47,100 and we'll give it a zero to start and then we want our end value to be ten. 106 00:10:48,240 --> 00:10:51,360 And they will say four are in range. 107 00:10:53,020 --> 00:10:54,280 If we want to print out. 108 00:10:59,190 --> 00:11:03,360 Iterator and we want this to be a little. 109 00:11:03,630 --> 00:11:04,150 Ah. 110 00:11:04,440 --> 00:11:10,410 So now if we run this and we now see zero one, two, three all the way to nine, which is perfect. 111 00:11:10,410 --> 00:11:16,290 So now we know that our iterator is working for our range struck. 112 00:11:17,800 --> 00:11:25,840 So I also said that we should get some default traits or excuse me, some default methods once we implement 113 00:11:25,840 --> 00:11:28,780 in the next function. 114 00:11:29,050 --> 00:11:31,690 So let's check to make sure that we did that. 115 00:11:31,690 --> 00:11:36,940 And then that would also prove that we got our. 116 00:11:38,490 --> 00:11:40,110 Implementation done correctly. 117 00:11:40,500 --> 00:11:45,330 So we're going to call range dot filter, which is a function given to us. 118 00:11:45,330 --> 00:11:53,430 If we implement NEX correctly, we're going to pass in a parameter and we only want values where when 119 00:11:53,430 --> 00:11:57,450 that parameter modded by two equals zero. 120 00:11:57,450 --> 00:12:05,880 So that means when the the values divided by two are remainder is zero and then we want to collect that 121 00:12:05,880 --> 00:12:09,300 way it is stored inside of our vector. 122 00:12:12,280 --> 00:12:19,240 And now if we print this vector out, we would expect all the values that when divided by two equals 123 00:12:19,240 --> 00:12:19,930 zero. 124 00:12:21,120 --> 00:12:27,360 And we see that we get 02468, which is correct. 125 00:12:27,570 --> 00:12:30,060 So that's really all there is to it. 126 00:12:30,060 --> 00:12:38,010 For integrators and closures, we see that we can use closures to allow us to easily perform logic on 127 00:12:38,010 --> 00:12:39,840 values inside an iterator. 128 00:12:40,290 --> 00:12:45,420 In the next section, let's continue on and start learning about smart pointers.