1 00:00:05,950 --> 00:00:06,370 All right. 2 00:00:06,370 --> 00:00:12,700 In this lecture, let's take a little bit of a more thorough look at how moves work. 3 00:00:13,000 --> 00:00:21,790 So the first thing we're going to do, we're going to create a string and say string from an we'll say 4 00:00:21,820 --> 00:00:27,640 takes because we're going to take ownership away from the string through a function. 5 00:00:27,640 --> 00:00:38,410 So we're going to call this F and takes ownership and we are going to take a string. 6 00:00:42,850 --> 00:00:43,660 And then. 7 00:00:43,660 --> 00:00:46,930 And here we will just say. 8 00:00:48,100 --> 00:00:48,610 Let. 9 00:00:52,610 --> 00:00:54,940 During equal se. 10 00:00:55,670 --> 00:00:56,150 Okay. 11 00:00:56,870 --> 00:01:02,000 So what's going to happen in this is I guess I should call it. 12 00:01:02,450 --> 00:01:14,030 So takes ownership and we pass in SX and then in here we'll print it out and we'll print out string. 13 00:01:15,980 --> 00:01:17,030 So what happens here? 14 00:01:17,030 --> 00:01:20,030 So we create a variable. 15 00:01:21,520 --> 00:01:26,350 With a string takes. 16 00:01:27,280 --> 00:01:34,600 And then in here, we give ownership to the function. 17 00:01:35,480 --> 00:01:35,900 Right. 18 00:01:35,900 --> 00:01:44,720 So we're taking this variable, giving it ownership inside of this function and we're using it in here. 19 00:01:44,720 --> 00:01:51,890 So if we cargo run it, we can see that we're in here inside this function. 20 00:01:52,040 --> 00:01:52,730 So. 21 00:01:53,610 --> 00:01:57,510 Just to make sure that the ownership was taken properly. 22 00:01:58,230 --> 00:02:07,050 Let's actually and to be lazy, I'm going to copy and paste and it's not even going to let us build 23 00:02:07,050 --> 00:02:07,560 and run. 24 00:02:08,160 --> 00:02:13,800 But again, earlier on in this lecture, we saw the same errors where it talks about how we move the 25 00:02:13,800 --> 00:02:22,800 value and then we try to reference the value borrowed after the move so we can see that we pass ownership 26 00:02:22,800 --> 00:02:27,870 to functions when we pass the values through parameters. 27 00:02:28,350 --> 00:02:29,820 So what about. 28 00:02:30,820 --> 00:02:33,340 Passing a value as a copy. 29 00:02:33,340 --> 00:02:36,310 So we'll say let value equals one. 30 00:02:37,400 --> 00:02:39,410 And then we'll we'll do the same thing. 31 00:02:39,410 --> 00:02:46,600 We'll just say, hey, how about make a copy of it? 32 00:02:47,300 --> 00:02:54,230 So then we'll call this one because that's what we're going to pass in and we'll just make it of I 32. 33 00:02:55,350 --> 00:03:04,650 And down here we'll say let vow one equals one. 34 00:03:06,000 --> 00:03:08,300 And we'll print out vowel one. 35 00:03:09,980 --> 00:03:14,300 And then here we will say make copy of vow. 36 00:03:14,690 --> 00:03:18,320 And we make a copy here because. 37 00:03:19,250 --> 00:03:24,830 This is an AI 32 which implements the copy trade. 38 00:03:24,860 --> 00:03:28,430 So again, if we run it, we'll see one printed out. 39 00:03:28,640 --> 00:03:31,850 And same concept applies. 40 00:03:31,850 --> 00:03:32,750 What we learned. 41 00:03:32,750 --> 00:03:35,030 Last lecture of. 42 00:03:36,250 --> 00:03:42,340 When it's a certain variable, some of them implement the copy trade. 43 00:03:43,520 --> 00:03:46,620 And I 32 implements copy. 44 00:03:46,730 --> 00:03:48,620 Therefore the function does not take. 45 00:03:49,750 --> 00:03:50,800 Ownership of it. 46 00:03:51,430 --> 00:03:53,740 So we can also give ownership. 47 00:03:53,740 --> 00:04:11,770 So we'll say give ownership and and here we'll do another string, string, one string and we want to 48 00:04:11,770 --> 00:04:14,020 pass it back a string. 49 00:04:14,350 --> 00:04:16,930 And then we will say. 50 00:04:20,230 --> 00:04:26,770 Given not to string. 51 00:04:28,690 --> 00:04:29,140 Got an error. 52 00:04:29,170 --> 00:04:29,620 What do we got? 53 00:04:37,140 --> 00:04:45,420 So up here we will have let type string equals. 54 00:04:46,560 --> 00:04:47,850 Give ownership. 55 00:04:49,150 --> 00:04:52,240 Oh, and we don't want to pass a parameter. 56 00:04:52,240 --> 00:04:54,910 And I see what my issue is down below. 57 00:04:57,410 --> 00:04:58,100 Get rid of that. 58 00:04:59,690 --> 00:05:07,190 So we called give ownership, which owns given it owns this, even though it's not technically a parameter, 59 00:05:07,190 --> 00:05:17,150 it still owns the value and we can print it out and show that we now have ownership of it. 60 00:05:17,300 --> 00:05:18,050 So. 61 00:05:21,760 --> 00:05:23,140 And it was given. 62 00:05:23,140 --> 00:05:28,480 So down here we passed ownership back up through a. 63 00:05:29,280 --> 00:05:30,030 Returning. 64 00:05:33,290 --> 00:05:36,920 So now we can also do both of them so we can. 65 00:05:38,180 --> 00:05:41,510 Take and give. 66 00:05:43,040 --> 00:05:45,740 So let's do another string. 67 00:05:48,360 --> 00:05:54,300 Call it string to string and it's going to return a string. 68 00:05:57,160 --> 00:06:04,510 And we'll just do that or return exactly what is given back. 69 00:06:05,800 --> 00:06:12,640 So we'll say let string three type string equals. 70 00:06:15,230 --> 00:06:19,360 Take and give but of string one. 71 00:06:19,370 --> 00:06:27,500 So now I expect string one to print out, given which it does, and to prove that we took it. 72 00:06:28,290 --> 00:06:32,630 Let's try printing out string one and see what happens. 73 00:06:32,640 --> 00:06:41,100 So we already got our air, which is a good sign so we can see that we are able to take and give ownership 74 00:06:41,100 --> 00:06:42,450 through functions. 75 00:06:42,690 --> 00:06:47,490 Well, what about with control flow statements? 76 00:06:48,560 --> 00:06:49,570 Do they work the same? 77 00:06:49,580 --> 00:06:55,910 So let's take a let's take a look at that just to see what is going to happen with those. 78 00:06:55,910 --> 00:07:07,700 So we'll again, keep super simple concepts here and we'll say let string four equals string three else. 79 00:07:09,050 --> 00:07:16,880 And this obviously never get run because we have true in the block above but just for sake of it string 80 00:07:16,880 --> 00:07:18,890 five also equals. 81 00:07:19,900 --> 00:07:20,830 Stream three. 82 00:07:24,190 --> 00:07:28,390 So what happens if we try to print out string three? 83 00:07:29,500 --> 00:07:30,490 It's given us that air. 84 00:07:30,880 --> 00:07:32,020 Sounds like a good sign. 85 00:07:32,290 --> 00:07:33,070 Let's see what it says. 86 00:07:33,070 --> 00:07:34,090 Move KERS. 87 00:07:34,270 --> 00:07:35,710 Because string three is type. 88 00:07:35,710 --> 00:07:40,530 String does not implement copy that checks out at line 35. 89 00:07:40,540 --> 00:07:42,100 The value is moved here. 90 00:07:43,000 --> 00:07:46,360 So also in control flow statements. 91 00:07:47,170 --> 00:07:50,260 Ownership operates the same as if they're not there. 92 00:07:51,220 --> 00:07:51,820 So. 93 00:07:53,070 --> 00:07:55,470 You can pass ownership through those as well. 94 00:07:57,830 --> 00:07:59,870 And lastly, let's look at a loop. 95 00:08:00,140 --> 00:08:04,220 So for this, let's just do a kind of a clean slate. 96 00:08:04,340 --> 00:08:05,630 Let's comment all this out. 97 00:08:07,240 --> 00:08:11,950 Comment all this out to suppress any airs or warnings? 98 00:08:11,950 --> 00:08:12,760 I should say. 99 00:08:13,120 --> 00:08:15,820 So let's create a mutable string to. 100 00:08:20,700 --> 00:08:22,290 And then let's just call. 101 00:08:23,110 --> 00:08:23,770 Loop. 102 00:08:25,930 --> 00:08:28,270 And I actually want to. 103 00:08:30,830 --> 00:08:38,030 He will give let mut str one equal. 104 00:08:40,530 --> 00:08:42,870 Let's drink from. 105 00:08:47,730 --> 00:08:48,450 Tyler. 106 00:08:49,820 --> 00:08:50,740 So a loop. 107 00:08:50,750 --> 00:08:55,220 Remember, this is just going to be an infinite loop that happens non-stop. 108 00:08:55,220 --> 00:09:00,380 So let's assign STR two to STR one. 109 00:09:00,920 --> 00:09:01,820 And what do we get? 110 00:09:03,850 --> 00:09:06,940 So we also have an ownership issue here. 111 00:09:06,940 --> 00:09:11,860 And this is actually really important to note because remember, this is an infinite loop. 112 00:09:11,860 --> 00:09:17,740 But imagine if you have a for loop or a while loop based on some kind of condition. 113 00:09:17,740 --> 00:09:19,030 Well, if you. 114 00:09:20,190 --> 00:09:27,090 Change ownership of a vowel of a variable or a value, I should say, inside a loop. 115 00:09:28,060 --> 00:09:33,520 That ownership changes so that ownership needs to be able to stay alive on that. 116 00:09:35,750 --> 00:09:42,560 Value for the entirety of the loop, because if you change ownership, you might break the loop, which 117 00:09:42,560 --> 00:09:48,830 is what happens in this case, because it says here the value was moved here and previous iteration 118 00:09:48,830 --> 00:09:49,940 of a loop. 119 00:09:50,720 --> 00:09:54,710 So those are just a couple of more examples of how ownership works. 120 00:09:54,710 --> 00:09:59,870 And I did not want to spend as much time typing out the codes as going over the concept. 121 00:09:59,870 --> 00:10:02,600 So that was why it was a lot of copying and pasting. 122 00:10:03,590 --> 00:10:07,700 But all of these concepts, we will practice a lot throughout this course. 123 00:10:07,700 --> 00:10:14,930 So I really just wanted to make sure I introduced them just to really emphasize how important these 124 00:10:14,930 --> 00:10:19,070 concepts are for understanding the rust language.