1 00:00:05,920 --> 00:00:13,570 This lecture is going to be a bit more of just talking and not much coding as these are important concepts 2 00:00:13,570 --> 00:00:14,650 to understand. 3 00:00:15,070 --> 00:00:19,450 With that, there are three traits that we need to cover. 4 00:00:19,840 --> 00:00:30,220 We have F an F and mute and F in once. 5 00:00:33,520 --> 00:00:41,320 Effen is the family of closures and functions that you can call multiple times without restrictions, 6 00:00:41,320 --> 00:00:46,210 and it borrows values from the environment immutably. 7 00:00:46,210 --> 00:00:57,880 And this is the highest category and it includes all little F, includes all F in functions. 8 00:00:59,740 --> 00:01:06,940 F in mute is the family of closures that can be called multiple times if the closure itself is declared 9 00:01:06,940 --> 00:01:10,270 mute and immutably borrows values. 10 00:01:11,540 --> 00:01:16,470 F and once is the family of closures that can be called once. 11 00:01:16,490 --> 00:01:24,680 If the caller owns the closure and then the once part of the name is because the closure cannot take 12 00:01:24,680 --> 00:01:28,220 ownership of the same variables more than once. 13 00:01:29,510 --> 00:01:39,210 So every big fan meets the requirements for fan mute and every FM mute meets the requirements for F 14 00:01:39,230 --> 00:01:40,040 and once. 15 00:01:40,280 --> 00:01:48,620 And this means that fan is a subtree of F and mute and f and mute is a sub trait of F and wants. 16 00:01:48,620 --> 00:01:56,840 Therefore we can conclude that f an is the most exclusive and most powerful trait of the three. 17 00:01:56,840 --> 00:02:03,110 So let's take a look at three separate closures and try to establish which trait each example falls 18 00:02:03,110 --> 00:02:03,590 under. 19 00:02:05,150 --> 00:02:12,800 So the first example is going to be a closure that takes no parameters and then drops a value. 20 00:02:13,460 --> 00:02:21,470 The second one is going to take an argument and then check to see if a vector contains the argument. 21 00:02:21,920 --> 00:02:30,140 And the last one is going to also take an ARG and then it's going to push into the vector that argh! 22 00:02:30,140 --> 00:02:34,820 So take a second and try to establish which trait goes with which example. 23 00:02:39,200 --> 00:02:39,590 All right. 24 00:02:39,590 --> 00:02:47,000 So the first example is going to be F and once because we can only drop the value once. 25 00:02:49,070 --> 00:02:57,680 The second example is going to implement often because all we're doing is checking to see if the vector 26 00:02:57,680 --> 00:03:00,680 contains the parameter that was passed in. 27 00:03:00,710 --> 00:03:05,660 There is nothing required in this that needs to be mutable. 28 00:03:05,960 --> 00:03:14,330 And then lastly, this is often mute because the vector would need to be mutable in order for us to 29 00:03:14,330 --> 00:03:17,510 push the argument into the vector. 30 00:03:19,870 --> 00:03:23,450 And that's really all there is to it for the F and trait. 31 00:03:23,470 --> 00:03:29,650 So now let's talk about copying and clone enclosures. 32 00:03:30,160 --> 00:03:37,120 The rules for copying and cloning work like the regular copy and clone and rest, will automatically 33 00:03:37,120 --> 00:03:43,540 figure out which closers can implement copy and clone and which closers cannot. 34 00:03:43,930 --> 00:03:46,530 So let's take a look at two simple examples. 35 00:03:46,540 --> 00:03:54,400 So for this one, we're going to create a value by assign it five, and then we're going to create a 36 00:03:54,400 --> 00:04:01,120 closure here that's going to take an argument and then add it to our Y value. 37 00:04:01,120 --> 00:04:06,910 And now we're going to take a copy of the add y closure we created. 38 00:04:07,090 --> 00:04:09,370 So this is. 39 00:04:10,750 --> 00:04:14,230 Closure being copied. 40 00:04:15,880 --> 00:04:24,310 And now we can check to make sure that our copy actually worked correctly so we can say add y and then 41 00:04:24,310 --> 00:04:30,670 in here we can also call copy and we'll give it the value ten. 42 00:04:31,680 --> 00:04:39,810 So if this copy works correctly, we would expect 20 to be printed out because here we're designated 43 00:04:39,810 --> 00:04:41,810 that the closure is being copied. 44 00:04:41,820 --> 00:04:50,910 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 45 00:04:50,910 --> 00:04:53,310 passed in of 15. 46 00:04:53,670 --> 00:04:55,740 So if we run this. 47 00:04:56,730 --> 00:05:01,290 We got the expected output there of 20. 48 00:05:02,800 --> 00:05:09,220 So now let's create an example where neither copy nor clone works. 49 00:05:09,220 --> 00:05:10,900 So we'll actually. 50 00:05:11,990 --> 00:05:14,150 Take this and we'll. 51 00:05:15,670 --> 00:05:19,330 Modify it a bit just to keep our example very similar. 52 00:05:19,570 --> 00:05:28,210 And we'll put mute Y and then we'll do a mutable ad y and then we'll also even make copy mutable. 53 00:05:29,050 --> 00:05:34,240 And then in here we are going to modify the logic being done. 54 00:05:34,240 --> 00:05:43,570 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 55 00:05:43,570 --> 00:05:45,070 already not going to work. 56 00:05:45,070 --> 00:05:46,720 But if we run it anyway. 57 00:05:48,480 --> 00:05:54,560 We see that the value was borrowed here after a move. 58 00:05:54,570 --> 00:06:03,300 And remember that this does not work because mutable values do not implement copy or clone. 59 00:06:03,390 --> 00:06:07,500 So that is why this does not work, even though our changes were very minute. 60 00:06:08,130 --> 00:06:12,240 But all in all, closures are not overly complicated. 61 00:06:12,240 --> 00:06:17,790 And I know that this the past few lectures were more theoretical than it was hands on. 62 00:06:18,420 --> 00:06:24,390 But we will actually see where closers shine and the next topic of iterations.