- This lesson is all about nested functions and closures.
To be honest, it's mostly about closures,
but we'll have a look at nested functions first,
which is a kind of simpler syntax.
It'll introduce some concepts
which will then lead us into closures later.
So you can define nested functions in Rust.
In other words, a function defined inside another.
The nested function can only be accessed
inside the function within which it's defined.
So here's a simple example.
I've got an outer function,
some_outer_function I've called it,
and inside, I've defined a nested function sqr.
It takes an integer,
a 32 bit integer as an input,
and it returns the square, like so.
This is a nested function.
You can only call this function
within the scope in which it's defined, okay?
So only this outer function can see this nested function,
and you call it like a regular function.
So I've called the nested function once.
I passed in the value five.
It returned 25.
The value 25 gets printed.
I call the function again.
I pass in the value seven into here.
It returns 49.
The value 49 gets printed, okay?
So it's very simple,
and not much you can say about it, really.
Some fun facts about nested functions.
You've got to specify full information
about the parameters and the return type
for a nested function,
and that seems quite straightforward.
Obviously, with a function
you have to say what's its parameters,
what's its return type.
Also, a nested function isn't allowed
to access variables defined in the outer scope.
So your nested function will live inside an outer function.
That outer function might declare some variables,
but you can't see them in the nested function.
The nested function can only see its parameters.
So if you had to get any values into the nested function
you've gotta pass them in as regular parameters.
Okay, so that all seems perfectly reasonable.
The reason I make these points
is because when we look at closures
for the rest of the lesson,
closures actually overcome these constraints.
As we'll see,
a closure doesn't have to specify explicitly
the types of the parameters in the return.
And as we'll see, a closure can access variables
declared in the outer scope.
It captures the variables declared in the outer scope
and can use them inside a closure.
Okay, so we'll have a look at closures
later on for the rest of the lesson.
But first of all,
let's just see a quick example of nested functions
before we go on any further.
So here it is.
The project is lesson09_nested_functions_closures.
Okay, so here it is in rustdev,
lesson09_nested_functions_closure.
I open that up in a code editor, and here we are.
Same arrangement as usual.
I've got my main code.
In my main code I'm going to uncomment the first demo.
Demo_nested_functions, right?
So here we are, nested functions.
Basically, the code that we saw in the slide.
Here's my nested function,
and I call it here,
and I call it here.
It is only visible inside this function.
So I guess we could just run it
and just confirm that it works.
Cargo run; So I'm running the application.
It's downloading some dependencies
that I need in my application.
In my TOML I've got a chronodependency.
I'm going to be doing some time related bits and pieces.
When you add the chrono crate into your project,
it pulls in various other bits and pieces as well.
And then, I called my demo_nested_functions,
which called the sqr function to get the square of five,
which I can confirm is 25,
and the square of seven is 49.
So there we go, a very simple nested function.
Quite limited, you know.
You've gotta give it full type information
for its parameters and its return.
It can't access any variables
that might have been declared in the outer scope,
and it's only visible inside this function.
What we're gonna do for the rest of the lesson
is see a similar kind of approach,
but instead of having nested function syntax,
we're going to replace this with closures,
which are a bit like lambdas in other languages
and much more powerful.