1 00:00:06,570 --> 00:00:11,040 - This lesson is all about nested functions and closures. 2 00:00:11,040 --> 00:00:13,800 To be honest, it's mostly about closures, 3 00:00:13,800 --> 00:00:15,690 but we'll have a look at nested functions first, 4 00:00:15,690 --> 00:00:17,700 which is a kind of simpler syntax. 5 00:00:17,700 --> 00:00:19,110 It'll introduce some concepts 6 00:00:19,110 --> 00:00:22,410 which will then lead us into closures later. 7 00:00:22,410 --> 00:00:25,350 So you can define nested functions in Rust. 8 00:00:25,350 --> 00:00:28,680 In other words, a function defined inside another. 9 00:00:28,680 --> 00:00:31,650 The nested function can only be accessed 10 00:00:31,650 --> 00:00:34,170 inside the function within which it's defined. 11 00:00:34,170 --> 00:00:35,850 So here's a simple example. 12 00:00:35,850 --> 00:00:37,265 I've got an outer function, 13 00:00:37,265 --> 00:00:39,750 some_outer_function I've called it, 14 00:00:39,750 --> 00:00:43,980 and inside, I've defined a nested function sqr. 15 00:00:43,980 --> 00:00:45,330 It takes an integer, 16 00:00:45,330 --> 00:00:48,360 a 32 bit integer as an input, 17 00:00:48,360 --> 00:00:51,090 and it returns the square, like so. 18 00:00:51,090 --> 00:00:53,100 This is a nested function. 19 00:00:53,100 --> 00:00:54,900 You can only call this function 20 00:00:54,900 --> 00:00:57,930 within the scope in which it's defined, okay? 21 00:00:57,930 --> 00:01:02,070 So only this outer function can see this nested function, 22 00:01:02,070 --> 00:01:04,170 and you call it like a regular function. 23 00:01:04,170 --> 00:01:06,840 So I've called the nested function once. 24 00:01:06,840 --> 00:01:08,940 I passed in the value five. 25 00:01:08,940 --> 00:01:10,830 It returned 25. 26 00:01:10,830 --> 00:01:12,390 The value 25 gets printed. 27 00:01:12,390 --> 00:01:13,770 I call the function again. 28 00:01:13,770 --> 00:01:16,380 I pass in the value seven into here. 29 00:01:16,380 --> 00:01:18,120 It returns 49. 30 00:01:18,120 --> 00:01:20,730 The value 49 gets printed, okay? 31 00:01:20,730 --> 00:01:22,290 So it's very simple, 32 00:01:22,290 --> 00:01:24,840 and not much you can say about it, really. 33 00:01:24,840 --> 00:01:27,750 Some fun facts about nested functions. 34 00:01:27,750 --> 00:01:30,720 You've got to specify full information 35 00:01:30,720 --> 00:01:33,210 about the parameters and the return type 36 00:01:33,210 --> 00:01:34,530 for a nested function, 37 00:01:34,530 --> 00:01:37,290 and that seems quite straightforward. 38 00:01:37,290 --> 00:01:38,400 Obviously, with a function 39 00:01:38,400 --> 00:01:40,380 you have to say what's its parameters, 40 00:01:40,380 --> 00:01:41,613 what's its return type. 41 00:01:43,110 --> 00:01:45,990 Also, a nested function isn't allowed 42 00:01:45,990 --> 00:01:49,110 to access variables defined in the outer scope. 43 00:01:49,110 --> 00:01:53,760 So your nested function will live inside an outer function. 44 00:01:53,760 --> 00:01:56,370 That outer function might declare some variables, 45 00:01:56,370 --> 00:01:59,460 but you can't see them in the nested function. 46 00:01:59,460 --> 00:02:02,400 The nested function can only see its parameters. 47 00:02:02,400 --> 00:02:05,100 So if you had to get any values into the nested function 48 00:02:05,100 --> 00:02:07,773 you've gotta pass them in as regular parameters. 49 00:02:08,910 --> 00:02:11,700 Okay, so that all seems perfectly reasonable. 50 00:02:11,700 --> 00:02:13,230 The reason I make these points 51 00:02:13,230 --> 00:02:15,090 is because when we look at closures 52 00:02:15,090 --> 00:02:16,500 for the rest of the lesson, 53 00:02:16,500 --> 00:02:19,350 closures actually overcome these constraints. 54 00:02:19,350 --> 00:02:20,430 As we'll see, 55 00:02:20,430 --> 00:02:24,240 a closure doesn't have to specify explicitly 56 00:02:24,240 --> 00:02:27,240 the types of the parameters in the return. 57 00:02:27,240 --> 00:02:31,290 And as we'll see, a closure can access variables 58 00:02:31,290 --> 00:02:32,970 declared in the outer scope. 59 00:02:32,970 --> 00:02:36,060 It captures the variables declared in the outer scope 60 00:02:36,060 --> 00:02:38,700 and can use them inside a closure. 61 00:02:38,700 --> 00:02:40,440 Okay, so we'll have a look at closures 62 00:02:40,440 --> 00:02:42,570 later on for the rest of the lesson. 63 00:02:42,570 --> 00:02:43,740 But first of all, 64 00:02:43,740 --> 00:02:45,810 let's just see a quick example of nested functions 65 00:02:45,810 --> 00:02:48,060 before we go on any further. 66 00:02:48,060 --> 00:02:48,900 So here it is. 67 00:02:48,900 --> 00:02:51,513 The project is lesson09_nested_functions_closures. 68 00:02:54,450 --> 00:02:57,450 Okay, so here it is in rustdev, 69 00:02:57,450 --> 00:02:58,673 lesson09_nested_functions_closure. 70 00:03:01,260 --> 00:03:06,260 I open that up in a code editor, and here we are. 71 00:03:06,420 --> 00:03:07,770 Same arrangement as usual. 72 00:03:07,770 --> 00:03:09,180 I've got my main code. 73 00:03:09,180 --> 00:03:13,410 In my main code I'm going to uncomment the first demo. 74 00:03:13,410 --> 00:03:16,800 Demo_nested_functions, right? 75 00:03:16,800 --> 00:03:18,660 So here we are, nested functions. 76 00:03:18,660 --> 00:03:21,329 Basically, the code that we saw in the slide. 77 00:03:21,329 --> 00:03:23,370 Here's my nested function, 78 00:03:23,370 --> 00:03:25,140 and I call it here, 79 00:03:25,140 --> 00:03:26,340 and I call it here. 80 00:03:26,340 --> 00:03:29,420 It is only visible inside this function. 81 00:03:29,420 --> 00:03:31,080 So I guess we could just run it 82 00:03:31,080 --> 00:03:32,643 and just confirm that it works. 83 00:03:34,860 --> 00:03:39,860 Cargo run; So I'm running the application. 84 00:03:39,960 --> 00:03:42,060 It's downloading some dependencies 85 00:03:42,060 --> 00:03:43,710 that I need in my application. 86 00:03:43,710 --> 00:03:46,680 In my TOML I've got a chronodependency. 87 00:03:46,680 --> 00:03:49,740 I'm going to be doing some time related bits and pieces. 88 00:03:49,740 --> 00:03:54,210 When you add the chrono crate into your project, 89 00:03:54,210 --> 00:03:57,480 it pulls in various other bits and pieces as well. 90 00:03:57,480 --> 00:04:00,390 And then, I called my demo_nested_functions, 91 00:04:00,390 --> 00:04:04,110 which called the sqr function to get the square of five, 92 00:04:04,110 --> 00:04:06,480 which I can confirm is 25, 93 00:04:06,480 --> 00:04:08,340 and the square of seven is 49. 94 00:04:08,340 --> 00:04:11,130 So there we go, a very simple nested function. 95 00:04:11,130 --> 00:04:13,470 Quite limited, you know. 96 00:04:13,470 --> 00:04:15,480 You've gotta give it full type information 97 00:04:15,480 --> 00:04:17,850 for its parameters and its return. 98 00:04:17,850 --> 00:04:19,650 It can't access any variables 99 00:04:19,650 --> 00:04:21,660 that might have been declared in the outer scope, 100 00:04:21,660 --> 00:04:24,240 and it's only visible inside this function. 101 00:04:24,240 --> 00:04:26,280 What we're gonna do for the rest of the lesson 102 00:04:26,280 --> 00:04:28,170 is see a similar kind of approach, 103 00:04:28,170 --> 00:04:31,050 but instead of having nested function syntax, 104 00:04:31,050 --> 00:04:33,390 we're going to replace this with closures, 105 00:04:33,390 --> 00:04:35,970 which are a bit like lambdas in other languages 106 00:04:35,970 --> 00:04:37,413 and much more powerful.