1 00:00:05,800 --> 00:00:12,460 Closures are anonymous functions, which are functions without names that we can save inside a variable 2 00:00:12,460 --> 00:00:15,160 or pass as an argument to other functions. 3 00:00:15,460 --> 00:00:17,410 The best way to learn this is by doing it. 4 00:00:17,410 --> 00:00:19,060 So let's get some code going. 5 00:00:19,540 --> 00:00:23,880 Because I believe the best way to learn closures is to see how they are useful. 6 00:00:23,890 --> 00:00:28,870 So let's create a sample program and then refactor in closures. 7 00:00:29,680 --> 00:00:33,820 So the first thing I want to do is create a new struct called City. 8 00:00:34,840 --> 00:00:43,000 And then here we're going to have the city name, which is a string, and then the population, which 9 00:00:43,000 --> 00:00:46,210 is going to be an unsigned 64. 10 00:00:49,740 --> 00:00:56,460 And then we're going to create a function called sort pop for sort population. 11 00:00:56,460 --> 00:01:01,650 And then in here we are going to take in a vector 12 00:01:04,320 --> 00:01:11,520 of type city and then we'll just call city dot sort. 13 00:01:13,530 --> 00:01:18,690 And we see that we're already getting the red squiggly line error. 14 00:01:18,690 --> 00:01:28,050 And if we hover over it and scroll down to the bottom, it says standard CFP for compare Ord is not 15 00:01:28,050 --> 00:01:29,230 satisfied. 16 00:01:29,250 --> 00:01:34,740 So for us to be able to do this sorting, we need to create a helper function. 17 00:01:35,910 --> 00:01:40,770 So I'll call it pop helper for population helper. 18 00:01:40,770 --> 00:01:50,220 And then in here we'll take our population and it will be a reference to a city and then it's going 19 00:01:50,220 --> 00:01:53,550 to return our unsigned 64. 20 00:01:54,920 --> 00:02:00,200 And then here, we'll just take our pop dot population. 21 00:02:02,140 --> 00:02:03,610 So now we can come back up here. 22 00:02:03,610 --> 00:02:11,950 And instead of saying sort, we'll say sort by key, and then we'll call our pop. 23 00:02:13,400 --> 00:02:15,050 Helper function. 24 00:02:16,820 --> 00:02:19,790 So now if we come down here, let's get some sample data going. 25 00:02:19,790 --> 00:02:24,340 We'll say and we'll just keep our cities very bland and not fun at all. 26 00:02:24,350 --> 00:02:26,270 We'll call and create a new city. 27 00:02:26,660 --> 00:02:27,800 Call it city. 28 00:02:28,040 --> 00:02:34,280 And our string from and our city name is going to be a very creative. 29 00:02:34,280 --> 00:02:34,850 I know. 30 00:02:35,150 --> 00:02:40,520 And then our pop insulation, we'll give it 100. 31 00:02:40,520 --> 00:02:42,260 It's a very small city. 32 00:02:43,860 --> 00:02:50,010 So now we'll do this and we'll just create a few of them and we'll go to. 33 00:02:51,820 --> 00:02:56,470 And then we'll be b c. 34 00:02:59,880 --> 00:03:00,540 The. 35 00:03:01,840 --> 00:03:02,710 An E. 36 00:03:03,730 --> 00:03:06,130 And then we'll just fill in here as well. 37 00:03:06,430 --> 00:03:12,680 C, D and E and let's change up our population some. 38 00:03:12,820 --> 00:03:14,290 We'll go 57. 39 00:03:15,130 --> 00:03:18,490 We'll go 140. 40 00:03:19,690 --> 00:03:27,220 We have a population of five and then our last city will have a population of 70. 41 00:03:27,250 --> 00:03:30,340 And then we'll create a new mutable vector 42 00:03:32,920 --> 00:03:34,300 of type city. 43 00:03:37,400 --> 00:03:41,510 And we will initialize it. 44 00:03:42,230 --> 00:03:51,080 And now we'll just push in all of our cities that we created. 45 00:03:55,720 --> 00:04:01,240 And we have B, C, D and E. 46 00:04:01,990 --> 00:04:10,630 So now let's call our sort population method and we'll pass in our vector. 47 00:04:10,930 --> 00:04:13,300 And then we want to be able to print out. 48 00:04:14,650 --> 00:04:15,940 Our vector sorted, 49 00:04:19,900 --> 00:04:20,950 which we can do. 50 00:04:21,900 --> 00:04:22,950 As such. 51 00:04:22,970 --> 00:04:24,600 It looks like we have a little air. 52 00:04:25,420 --> 00:04:25,660 Oh. 53 00:04:25,800 --> 00:04:28,080 Derive debug. 54 00:04:30,060 --> 00:04:37,310 So we will add that in and now our error is gone. 55 00:04:38,070 --> 00:04:38,490 Awesome. 56 00:04:38,490 --> 00:04:44,250 So now if we run this, we expect our vector to be sorted from. 57 00:04:47,220 --> 00:04:54,420 Population to graze, which we can see population 1557 7101 40. 58 00:04:55,130 --> 00:04:59,960 So we see that it is sorted in ascending order, which is what we wanted. 59 00:04:59,960 --> 00:05:04,070 But now let's see how we could have done this using a closure. 60 00:05:04,310 --> 00:05:09,230 So what we can do is come here and we'll just go ahead and just comment those two out. 61 00:05:10,460 --> 00:05:16,630 And now we'll create a new function called sort pop and then we'll just put closure in here. 62 00:05:16,640 --> 00:05:18,290 That way we can differentiate. 63 00:05:19,950 --> 00:05:28,470 Our functions and we'll have a reference to immutable vector again of type city. 64 00:05:29,700 --> 00:05:33,690 And then in here we will say pop sort by key. 65 00:05:33,690 --> 00:05:37,170 So the same function and now we will create our closure. 66 00:05:37,410 --> 00:05:39,960 So we'll have a. 67 00:05:42,360 --> 00:05:45,030 The two vertical lines. 68 00:05:45,030 --> 00:05:53,250 And then inside we'll have P and in this case, p is going to be our argument into the closure. 69 00:05:54,420 --> 00:06:00,000 And then we'll do pop or excuse me, p dot population. 70 00:06:01,860 --> 00:06:10,590 So we're passing in our P parameter and now we have access to the population which is obviously inside 71 00:06:10,590 --> 00:06:12,030 of our city struct. 72 00:06:12,450 --> 00:06:14,340 And now if we run it again. 73 00:06:17,670 --> 00:06:18,270 Oop. 74 00:06:22,600 --> 00:06:25,450 Sought closure. 75 00:06:27,450 --> 00:06:28,200 Here we go. 76 00:06:29,150 --> 00:06:35,970 And now if we run it, we see that we get the same exact output with the order being in ascending order. 77 00:06:37,280 --> 00:06:43,130 So this is just was a super simple way of showing where closures can help us out. 78 00:06:43,970 --> 00:06:52,340 We were able to go from this was a simple example, but from two functions down to one. 79 00:06:52,340 --> 00:06:57,140 And we were able to contain all of our logic inside of one line. 80 00:06:57,530 --> 00:06:59,810 Obviously, closures can do much more. 81 00:06:59,810 --> 00:07:08,660 So let's look at how we can further use closures and a little bit more of the details into how closures 82 00:07:08,660 --> 00:07:09,230 work.