1 00:00:06,587 --> 00:00:10,140 - In the previous section, we introduced nested functions, 2 00:00:10,140 --> 00:00:11,520 and for the rest of the lesson, 3 00:00:11,520 --> 00:00:14,610 we're going to switch our attention to closures. 4 00:00:14,610 --> 00:00:18,990 So a closure in Rust is kind of the same idea as a lambda 5 00:00:18,990 --> 00:00:20,760 in other languages. 6 00:00:20,760 --> 00:00:22,830 It's similar to a nested function, 7 00:00:22,830 --> 00:00:25,230 but the syntax is kind of different. 8 00:00:25,230 --> 00:00:28,860 And closures are more flexible 9 00:00:28,860 --> 00:00:31,230 than nested functions in a couple of ways. 10 00:00:31,230 --> 00:00:34,860 First of all, a closure can infer the types 11 00:00:34,860 --> 00:00:37,620 of its parameters and its return type, 12 00:00:37,620 --> 00:00:40,710 whereas a nested function, you have to specify 13 00:00:40,710 --> 00:00:43,500 the parameter types and the return types explicitly. 14 00:00:43,500 --> 00:00:45,480 With a closure or lambda, 15 00:00:45,480 --> 00:00:47,730 the compiler can guess what the types are, 16 00:00:47,730 --> 00:00:50,700 so it makes your code a little bit more concise. 17 00:00:50,700 --> 00:00:55,200 More importantly, a closure can capture external variables. 18 00:00:55,200 --> 00:00:59,190 So if you have a closure defined inside an outer function, 19 00:00:59,190 --> 00:01:01,590 the outer function might have some variables. 20 00:01:01,590 --> 00:01:04,260 You can see those variables in the closure. 21 00:01:04,260 --> 00:01:06,900 The closure can capture the variables defined 22 00:01:06,900 --> 00:01:08,610 in outer scope, and that's very powerful, 23 00:01:08,610 --> 00:01:10,650 we'll have a look at that later. 24 00:01:10,650 --> 00:01:14,180 So syntax-wise, the syntax we defined in the closure 25 00:01:14,180 --> 00:01:18,540 is similar but simpler than defining a nested function. 26 00:01:18,540 --> 00:01:22,980 So with a closure, it starts with vertical bars, pipes. 27 00:01:22,980 --> 00:01:26,190 And inside the pipes, you can define the parameters 28 00:01:26,190 --> 00:01:28,020 that you pass into the closure, remember, 29 00:01:28,020 --> 00:01:30,450 a closure is really like a nested function. 30 00:01:30,450 --> 00:01:32,430 And inside the vertical pipes, 31 00:01:32,430 --> 00:01:34,170 you can specify the parameters 32 00:01:34,170 --> 00:01:37,560 that the function takes, that the closure takes. 33 00:01:37,560 --> 00:01:41,040 Optionally, you can specify the types of those parameters 34 00:01:41,040 --> 00:01:43,800 and the type of the value returned from the closure. 35 00:01:43,800 --> 00:01:45,660 You can leave that out if you want to 36 00:01:45,660 --> 00:01:47,880 and the compiler can guess. 37 00:01:47,880 --> 00:01:51,300 And then the body of the closure, the actual kind of the, 38 00:01:51,300 --> 00:01:53,700 you know, the nuts and bolts of the function, if you like, 39 00:01:53,700 --> 00:01:55,500 you enclose inside curly brackets, 40 00:01:55,500 --> 00:01:58,320 just like you would for a regular function. 41 00:01:58,320 --> 00:02:00,960 So once you've defined a closure, 42 00:02:00,960 --> 00:02:03,780 you can assign that closure to a variable, 43 00:02:03,780 --> 00:02:05,430 and then you can use that variable 44 00:02:05,430 --> 00:02:07,290 as if it were the function name. 45 00:02:07,290 --> 00:02:10,620 You can use that variable to invoke the closure 46 00:02:10,620 --> 00:02:14,070 and pass parameters in and get the return value back. 47 00:02:14,070 --> 00:02:16,260 Right, so here's a simple example, 48 00:02:16,260 --> 00:02:18,990 a closure that doesn't take any parameters. 49 00:02:18,990 --> 00:02:23,990 So it is a closure and it returns a DateTime. 50 00:02:25,380 --> 00:02:28,260 DateTime is just a standard Rust type. 51 00:02:28,260 --> 00:02:31,890 It takes a generic type parameter, the time zone, basically. 52 00:02:31,890 --> 00:02:36,210 This closure will return a DateTime 53 00:02:36,210 --> 00:02:39,000 using universal coordinated time. 54 00:02:39,000 --> 00:02:42,510 The body of the closure is implicitly 55 00:02:42,510 --> 00:02:44,580 the return value it returns. 56 00:02:44,580 --> 00:02:46,020 When you call this lambda, 57 00:02:46,020 --> 00:02:48,870 it'll return the current time expressed 58 00:02:48,870 --> 00:02:50,790 in Universal Coordinated Time. 59 00:02:50,790 --> 00:02:53,130 Now, this variable here, get_timestamp, 60 00:02:53,130 --> 00:02:56,970 is basically a pointer to this function, if you like. 61 00:02:56,970 --> 00:02:59,970 So to invoke this closure, 62 00:02:59,970 --> 00:03:02,070 you just basically use the variable name. 63 00:03:02,070 --> 00:03:04,710 This variable name is effectively a pointer 64 00:03:04,710 --> 00:03:07,380 to the closure, call that closure. 65 00:03:07,380 --> 00:03:11,250 It doesn't take any parameters. There's an empty pipe here. 66 00:03:11,250 --> 00:03:16,250 So we call the lambda or the closure without any parameters. 67 00:03:16,290 --> 00:03:19,830 We call the closure, it returns the current time, 68 00:03:19,830 --> 00:03:21,660 we then print out the current time. 69 00:03:21,660 --> 00:03:25,290 So this is a simple closure, doesn't take any parameters. 70 00:03:25,290 --> 00:03:27,150 Of course, you can take parameters 71 00:03:27,150 --> 00:03:29,820 in a closure and you typically will. 72 00:03:29,820 --> 00:03:34,020 So here's an example of a closure that takes one parameter. 73 00:03:34,020 --> 00:03:38,040 So it's a closure, the pipes indicate that. 74 00:03:38,040 --> 00:03:41,910 It takes a 64-bit float as an input, 75 00:03:41,910 --> 00:03:45,330 like a function that takes a 64-bit float. 76 00:03:45,330 --> 00:03:49,950 It also returns a 64-bit float, and what does it return? 77 00:03:49,950 --> 00:03:53,490 Well, if the number you've passed in is zero, 78 00:03:53,490 --> 00:03:55,830 then it just returns zero, otherwise, 79 00:03:55,830 --> 00:03:57,960 it returns one divided by, 80 00:03:57,960 --> 00:03:59,520 it's trying to return the reciprocal. 81 00:03:59,520 --> 00:04:04,520 So for example, if I call this closure with a value five, 82 00:04:04,860 --> 00:04:07,140 the value five gets passed in here, 83 00:04:07,140 --> 00:04:11,873 and it returns one divided by five, which is 0.2, okay? 84 00:04:13,200 --> 00:04:15,930 So here is my closure. 85 00:04:15,930 --> 00:04:18,990 Effectively a function that takes a float. 86 00:04:18,990 --> 00:04:22,170 So I've passed in a float, five gets passed into here. 87 00:04:22,170 --> 00:04:27,170 It returns 0.2, and that return value is then displayed. 88 00:04:28,140 --> 00:04:31,230 So here's a closure that takes one parameter. 89 00:04:31,230 --> 00:04:34,020 A closure can take as many parameters as you like. 90 00:04:34,020 --> 00:04:36,450 So here's a closure that takes two parameters. 91 00:04:36,450 --> 00:04:40,500 As it happens, they're both 32-bit integers. 92 00:04:40,500 --> 00:04:43,380 It returns a 32-bit integer as well. 93 00:04:43,380 --> 00:04:44,703 It returns the product. 94 00:04:45,660 --> 00:04:48,360 Obviously, you would never bother writing this 95 00:04:48,360 --> 00:04:51,630 as a closure or a lambda, I'm just trying 96 00:04:51,630 --> 00:04:53,400 to illustrate the syntax, we'll have a look 97 00:04:53,400 --> 00:04:55,980 at more realistic examples later on. 98 00:04:55,980 --> 00:04:59,640 But syntax-wise, it's a closure that takes two integers, 99 00:04:59,640 --> 00:05:01,830 returns an integer, it returns the product 100 00:05:01,830 --> 00:05:02,970 of those two numbers. 101 00:05:02,970 --> 00:05:06,450 So to call the closure, when you call the closure, 102 00:05:06,450 --> 00:05:07,650 I've called it prod. 103 00:05:07,650 --> 00:05:10,920 This variable is a pointer to that closure, 104 00:05:10,920 --> 00:05:15,920 so call that closure, pass 20 into a, pass five into b, 105 00:05:16,800 --> 00:05:19,110 it returns 100, and that return value 106 00:05:19,110 --> 00:05:20,613 is then printed like so. 107 00:05:21,690 --> 00:05:26,373 Right, so what about a closure that had multiple statements? 108 00:05:27,360 --> 00:05:29,400 Fair enough, you just have as many statements 109 00:05:29,400 --> 00:05:31,450 as you want to inside the curly brackets. 110 00:05:32,520 --> 00:05:36,750 The last statement here is, if it's an expression, 111 00:05:36,750 --> 00:05:39,150 no semicolon, in other words, this expression 112 00:05:39,150 --> 00:05:41,430 is assumed to be the return statement. 113 00:05:41,430 --> 00:05:44,430 So here is a closure. 114 00:05:44,430 --> 00:05:46,890 It takes in a number of seconds 115 00:05:46,890 --> 00:05:49,173 as a 64-bit unsigned integer. 116 00:05:50,100 --> 00:05:53,790 It returns the date and time in UTC. 117 00:05:53,790 --> 00:05:56,880 When I invoke this closure, let's see. 118 00:05:56,880 --> 00:06:00,360 I invoke the closure, get_timestamp_after_delay. 119 00:06:00,360 --> 00:06:03,513 I pass five as the number of seconds. 120 00:06:04,350 --> 00:06:07,500 It sleeps for a duration of five seconds 121 00:06:07,500 --> 00:06:11,400 and zero nanoseconds, and then it returns 122 00:06:11,400 --> 00:06:15,300 the time after that delay, the DateTime, 123 00:06:15,300 --> 00:06:17,760 and that DateTime is then displayed down here. 124 00:06:17,760 --> 00:06:21,340 If I wanted to, I could have explicitly said return 125 00:06:23,970 --> 00:06:26,880 with a semicolon at the end, but it's common 126 00:06:26,880 --> 00:06:29,040 just to have an expression, and that expression 127 00:06:29,040 --> 00:06:31,260 is implicitly the return value 128 00:06:31,260 --> 00:06:34,440 from the function or from the closure. 129 00:06:34,440 --> 00:06:36,210 Right, example time. 130 00:06:36,210 --> 00:06:38,970 So back to our demo project, lesson nine, 131 00:06:38,970 --> 00:06:41,310 nested functions, closures. 132 00:06:41,310 --> 00:06:44,913 The demo closures example, which we're going to run. 133 00:06:46,020 --> 00:06:48,360 Okay, here we are then, so in main, 134 00:06:48,360 --> 00:06:52,020 let's uncomment demo closures 135 00:06:52,020 --> 00:06:55,560 and let's have a look at it here. 136 00:06:55,560 --> 00:06:58,140 So I've got an example 137 00:06:58,140 --> 00:07:00,540 of a closure that takes no parameters, 138 00:07:00,540 --> 00:07:02,790 a closure that takes one parameter, 139 00:07:02,790 --> 00:07:04,950 a closure that takes many parameters, 140 00:07:04,950 --> 00:07:06,900 and a closure that has multiple statements, 141 00:07:06,900 --> 00:07:09,210 basically, it's the code that we've just been looking at, 142 00:07:09,210 --> 00:07:11,940 but I'm gonna actually run it for real. 143 00:07:11,940 --> 00:07:14,760 I've imported, well, first of all, 144 00:07:14,760 --> 00:07:19,530 I've imported the DateTime and the UTC types 145 00:07:19,530 --> 00:07:21,000 from the chrono crate. 146 00:07:21,000 --> 00:07:23,640 Obviously, that means I've got to drag in 147 00:07:23,640 --> 00:07:25,623 the chrono crate here, which I have. 148 00:07:26,460 --> 00:07:30,120 And I'm also going to use the duration structure, 149 00:07:30,120 --> 00:07:31,530 a duration for a sleep. 150 00:07:31,530 --> 00:07:33,210 And I'm going to use the sleep function 151 00:07:33,210 --> 00:07:36,750 from the thread module in the standard crate. 152 00:07:36,750 --> 00:07:39,480 Well, we can just run it and have a look at the output 153 00:07:39,480 --> 00:07:42,360 and then we can kind of dissect the code afterwards, 154 00:07:42,360 --> 00:07:43,323 cargo run. 155 00:07:45,750 --> 00:07:47,400 I've got some delays in here, okay, 156 00:07:47,400 --> 00:07:50,643 so there are going to be some delays that we see popping up. 157 00:07:52,170 --> 00:07:57,170 Okay, so from the top, here is a closure, or rather, 158 00:07:57,780 --> 00:08:02,010 here is a closure that doesn't take any parameters. 159 00:08:02,010 --> 00:08:07,010 So when I call it, it'll return the timestamp like this, 160 00:08:07,110 --> 00:08:10,860 date, time, and time zone. 161 00:08:10,860 --> 00:08:15,860 If I wanted to, I could have instead formatted it like so, 162 00:08:15,960 --> 00:08:20,230 so when I get back the DateTime, I can just format it 163 00:08:20,230 --> 00:08:23,730 as a time if I wanted to rather than the whole DateTime. 164 00:08:23,730 --> 00:08:25,050 But the main thing is here, 165 00:08:25,050 --> 00:08:27,993 I'm calling the closure without any parameters. 166 00:08:29,400 --> 00:08:31,920 And here's a closure that takes a parameter, 167 00:08:31,920 --> 00:08:35,310 so this closure is the reciprocal, I called it. 168 00:08:35,310 --> 00:08:37,560 So reciprocal, remember, is just a variable. 169 00:08:37,560 --> 00:08:42,180 It refers to or points to this closure. 170 00:08:42,180 --> 00:08:43,560 It's like a function pointer. 171 00:08:43,560 --> 00:08:46,260 I invoke that closure with a value five 172 00:08:46,260 --> 00:08:51,060 and it returns one divided by five, which is 0.2. 173 00:08:51,060 --> 00:08:54,180 And then a closure that takes many parameters. 174 00:08:54,180 --> 00:08:56,280 So like a function, when you call it, 175 00:08:56,280 --> 00:08:57,990 you've gotta pass in two parameters, 176 00:08:57,990 --> 00:09:02,430 two integers, 20 and five, and it returns. 177 00:09:02,430 --> 00:09:07,430 The return type is i32 and the return value is 100, 178 00:09:07,620 --> 00:09:09,780 and that's what's displayed here. 179 00:09:09,780 --> 00:09:14,780 And then finally, a lambda, a multi-statement lambda. 180 00:09:14,820 --> 00:09:17,820 I pass in a number of seconds. 181 00:09:17,820 --> 00:09:20,100 So for example, I pass in the number five. 182 00:09:20,100 --> 00:09:23,310 It waits for five seconds, and it did when I ran it, 183 00:09:23,310 --> 00:09:26,760 and then it returns the timestamp afterwards, which I then, 184 00:09:26,760 --> 00:09:29,340 so that basically is a DateTime, 185 00:09:29,340 --> 00:09:31,950 which I then format just as a time, 186 00:09:31,950 --> 00:09:36,180 and it displays the time like this, must be time for dinner. 187 00:09:36,180 --> 00:09:38,550 So there we go, okay, so that's basically a simple example 188 00:09:38,550 --> 00:09:40,620 of closures to show the syntax 189 00:09:40,620 --> 00:09:43,350 of a closure that takes no parameters, 190 00:09:43,350 --> 00:09:45,720 a closure that takes one parameter, 191 00:09:45,720 --> 00:09:47,820 a closure that takes many parameters, 192 00:09:47,820 --> 00:09:50,520 and then a closure that spans multiple statements. 193 00:09:50,520 --> 00:09:52,800 In these examples, I've been specifying 194 00:09:52,800 --> 00:09:55,710 the types explicitly, I've been saying the parameters 195 00:09:55,710 --> 00:09:59,520 are explicitly int 32s and so is the return type. 196 00:09:59,520 --> 00:10:01,320 I've specified the parameter 197 00:10:01,320 --> 00:10:03,870 and the return type there explicitly. 198 00:10:03,870 --> 00:10:05,490 What we'll do in the next demo 199 00:10:05,490 --> 00:10:08,400 is see how we can omit type information 200 00:10:08,400 --> 00:10:13,260 and the compiler can guess what the type information is. 201 00:10:13,260 --> 00:10:14,223 So that's to come.