1 00:00:06,540 --> 00:00:08,730 - Hello, again, and welcome to lesson nine. 2 00:00:08,730 --> 00:00:10,980 This lesson continues the theme of functions, 3 00:00:10,980 --> 00:00:14,370 and shows how to define nested functions and closures. 4 00:00:14,370 --> 00:00:16,140 Nested functions are quite straightforward, 5 00:00:16,140 --> 00:00:17,760 so we'll cover them first. 6 00:00:17,760 --> 00:00:20,520 Then we'll spend the rest of the lesson looking at closures, 7 00:00:20,520 --> 00:00:23,430 which are similar to Lambdas in other languages. 8 00:00:23,430 --> 00:00:26,250 We'll have a look at the syntax of simple closures first 9 00:00:26,250 --> 00:00:29,580 using full closure syntax so you understand the idea. 10 00:00:29,580 --> 00:00:31,830 Then we'll see how you can use tight inference 11 00:00:31,830 --> 00:00:33,303 to simplify the syntax. 12 00:00:34,530 --> 00:00:38,130 Closures are called closures because they can capture 13 00:00:38,130 --> 00:00:41,700 or enclose variables defined in external scope. 14 00:00:41,700 --> 00:00:43,980 We'll see how to capture variables by reference, 15 00:00:43,980 --> 00:00:47,610 first of all, which is borrowing, invest. 16 00:00:47,610 --> 00:00:50,100 Then we'll see how to capture variables by value 17 00:00:50,100 --> 00:00:53,820 so that the closure claims ownership of the variable. 18 00:00:53,820 --> 00:00:56,520 We wrap up the lesson with a look at how to iterate 19 00:00:56,520 --> 00:00:59,850 over collection and perform an operation on each element, 20 00:00:59,850 --> 00:01:01,320 which is quite a good example 21 00:01:01,320 --> 00:01:04,083 of why to use closures in practice.