- Hello, again, and welcome to lesson nine.
This lesson continues the theme of functions,
and shows how to define nested functions and closures.
Nested functions are quite straightforward,
so we'll cover them first.
Then we'll spend the rest of the lesson looking at closures,
which are similar to Lambdas in other languages.
We'll have a look at the syntax of simple closures first
using full closure syntax so you understand the idea.
Then we'll see how you can use tight inference
to simplify the syntax.
Closures are called closures because they can capture
or enclose variables defined in external scope.
We'll see how to capture variables by reference,
first of all, which is borrowing, invest.
Then we'll see how to capture variables by value
so that the closure claims ownership of the variable.
We wrap up the lesson with a look at how to iterate
over collection and perform an operation on each element,
which is quite a good example
of why to use closures in practice.