This lecture is going to be a bit more of just talking and not much coding as these are important concepts
to understand.
With that, there are three traits that we need to cover.
We have F an F and mute and F in once.
Effen is the family of closures and functions that you can call multiple times without restrictions,
and it borrows values from the environment immutably.
And this is the highest category and it includes all little F, includes all F in functions.
F in mute is the family of closures that can be called multiple times if the closure itself is declared
mute and immutably borrows values.
F and once is the family of closures that can be called once.
If the caller owns the closure and then the once part of the name is because the closure cannot take
ownership of the same variables more than once.
So every big fan meets the requirements for fan mute and every FM mute meets the requirements for F
and once.
And this means that fan is a subtree of F and mute and f and mute is a sub trait of F and wants.
Therefore we can conclude that f an is the most exclusive and most powerful trait of the three.
So let's take a look at three separate closures and try to establish which trait each example falls
under.
So the first example is going to be a closure that takes no parameters and then drops a value.
The second one is going to take an argument and then check to see if a vector contains the argument.
And the last one is going to also take an ARG and then it's going to push into the vector that argh!
So take a second and try to establish which trait goes with which example.
All right.
So the first example is going to be F and once because we can only drop the value once.
The second example is going to implement often because all we're doing is checking to see if the vector
contains the parameter that was passed in.
There is nothing required in this that needs to be mutable.
And then lastly, this is often mute because the vector would need to be mutable in order for us to
push the argument into the vector.
And that's really all there is to it for the F and trait.
So now let's talk about copying and clone enclosures.
The rules for copying and cloning work like the regular copy and clone and rest, will automatically
figure out which closers can implement copy and clone and which closers cannot.
So let's take a look at two simple examples.
So for this one, we're going to create a value by assign it five, and then we're going to create a
closure here that's going to take an argument and then add it to our Y value.
And now we're going to take a copy of the add y closure we created.
So this is.
Closure being copied.
And now we can check to make sure that our copy actually worked correctly so we can say add y and then
in here we can also call copy and we'll give it the value ten.
So if this copy works correctly, we would expect 20 to be printed out because here we're designated
that the closure is being copied.
We are going to add 5 to 10 and then on the outer call, we're going to add five to the now value being
passed in of 15.
So if we run this.
We got the expected output there of 20.
So now let's create an example where neither copy nor clone works.
So we'll actually.
Take this and we'll.
Modify it a bit just to keep our example very similar.
And we'll put mute Y and then we'll do a mutable ad y and then we'll also even make copy mutable.
And then in here we are going to modify the logic being done.
So we're going to say Y is going to plus equal X and then we want to return Y and we see that this is
already not going to work.
But if we run it anyway.
We see that the value was borrowed here after a move.
And remember that this does not work because mutable values do not implement copy or clone.
So that is why this does not work, even though our changes were very minute.
But all in all, closures are not overly complicated.
And I know that this the past few lectures were more theoretical than it was hands on.
But we will actually see where closers shine and the next topic of iterations.