1 00:00:06,600 --> 00:00:09,090 - This lesson is all about what happens when you pass things 2 00:00:09,090 --> 00:00:11,880 into a function and then you when you return things 3 00:00:11,880 --> 00:00:13,140 from a function. 4 00:00:13,140 --> 00:00:16,530 The simplest case is when you pass something by value. 5 00:00:16,530 --> 00:00:18,120 So that's what we look at first. 6 00:00:18,120 --> 00:00:19,890 I've got a function, f1. 7 00:00:19,890 --> 00:00:23,490 It takes two parameters, it takes a 32-bit integer 8 00:00:23,490 --> 00:00:25,350 and it takes a string. 9 00:00:25,350 --> 00:00:28,650 Interesting difference between these two parameters. 10 00:00:28,650 --> 00:00:31,683 The i32 type implements copy. 11 00:00:32,520 --> 00:00:35,580 And when you pass by value, 12 00:00:35,580 --> 00:00:38,193 it'll be copied into the function. 13 00:00:39,330 --> 00:00:42,960 String doesn't implement copy. 14 00:00:42,960 --> 00:00:45,270 So when you pass a string value, 15 00:00:45,270 --> 00:00:48,990 you actually move ownership of the string into the function. 16 00:00:48,990 --> 00:00:51,060 It's all to do with the copy trait. 17 00:00:51,060 --> 00:00:53,430 So that's what we're gonna focus on here. 18 00:00:53,430 --> 00:00:57,330 How exactly does the parameter get passed into the function? 19 00:00:57,330 --> 00:01:00,210 So here's my higher level function. 20 00:01:00,210 --> 00:01:04,590 I've declared an integer n and I pass n into here. 21 00:01:04,590 --> 00:01:08,100 What we'll see is that n will just be bit copied 22 00:01:08,100 --> 00:01:10,023 into here and that's easy. 23 00:01:11,070 --> 00:01:13,050 The second parameter is a string. 24 00:01:13,050 --> 00:01:15,390 When you pass a string by value, 25 00:01:15,390 --> 00:01:19,530 it actually moves ownership of the string up to here. 26 00:01:19,530 --> 00:01:22,890 The original function loses ownership of the text 27 00:01:22,890 --> 00:01:25,950 and the receiving function claims ownership of the text. 28 00:01:25,950 --> 00:01:27,480 So you move ownership 29 00:01:27,480 --> 00:01:30,660 into the function and obviously, that has consequences. 30 00:01:30,660 --> 00:01:32,370 So we're gonna look at an example 31 00:01:32,370 --> 00:01:35,340 just to kind of work through that theory. 32 00:01:35,340 --> 00:01:37,050 So what we'll do, first of all, is we'll think 33 00:01:37,050 --> 00:01:40,743 about simple types which implement copy like i32. 34 00:01:41,790 --> 00:01:44,190 When you pass a copyable value, 35 00:01:44,190 --> 00:01:48,510 it is literally just bit copied into the parameter. 36 00:01:48,510 --> 00:01:51,390 The original function still retains ownership 37 00:01:51,390 --> 00:01:52,980 of the original variable. 38 00:01:52,980 --> 00:01:55,350 It's just a copy that gets passed in. 39 00:01:55,350 --> 00:01:57,450 So here's a simple example. 40 00:01:57,450 --> 00:02:00,720 In my high-level function, I've declared n. 41 00:02:00,720 --> 00:02:02,250 N is a simple integer. 42 00:02:02,250 --> 00:02:06,420 So I'll draw it here, 42. 43 00:02:06,420 --> 00:02:10,050 Obviously, when I pass n into the function, 44 00:02:10,050 --> 00:02:14,580 the value of n is bit copied into the parameter. 45 00:02:14,580 --> 00:02:19,580 So iparam comes into being iparam and it's a bit copy. 46 00:02:21,510 --> 00:02:23,280 There's number 42. 47 00:02:23,280 --> 00:02:24,600 It prints out the value. 48 00:02:24,600 --> 00:02:26,070 At the end of the function, 49 00:02:26,070 --> 00:02:29,820 obviously the local iparam is popped off the stack. 50 00:02:29,820 --> 00:02:31,410 It's gone. 51 00:02:31,410 --> 00:02:35,490 Back in the main function, n still owns the variable. 52 00:02:35,490 --> 00:02:38,310 When n was passed in, a copy was passed in 53 00:02:38,310 --> 00:02:41,460 but n continued to kind of own its own data. 54 00:02:41,460 --> 00:02:42,540 So that's fine. 55 00:02:42,540 --> 00:02:45,840 You can still use n after the function has returned. 56 00:02:45,840 --> 00:02:49,113 N still exists and it still owns its own value. 57 00:02:51,120 --> 00:02:53,160 Gets a bit more tricky when you have types 58 00:02:53,160 --> 00:02:56,730 that don't implement copy like string. 59 00:02:56,730 --> 00:03:00,870 So when you pass a non-copy type into a function, 60 00:03:00,870 --> 00:03:03,270 it moves ownership of that value 61 00:03:03,270 --> 00:03:06,030 into the second function. 62 00:03:06,030 --> 00:03:09,360 The original function loses ownership of the text. 63 00:03:09,360 --> 00:03:11,340 It's a move operation. 64 00:03:11,340 --> 00:03:15,150 Effectively, you donate your value into the function. 65 00:03:15,150 --> 00:03:17,700 The function then owns the value. 66 00:03:17,700 --> 00:03:19,980 The original function has lost ownership 67 00:03:19,980 --> 00:03:22,770 and can't be used after the function call. 68 00:03:22,770 --> 00:03:24,870 So it's quite dramatic actually. 69 00:03:24,870 --> 00:03:26,733 Let's see how that actually works. 70 00:03:27,900 --> 00:03:32,900 Right, so in my higher-level function, s is a string 71 00:03:33,180 --> 00:03:37,710 and it has a pointer to, on the heap, it holds some text. 72 00:03:37,710 --> 00:03:41,010 Hello. Like so. 73 00:03:41,010 --> 00:03:44,400 Now, remember, string doesn't implement the copy trait. 74 00:03:44,400 --> 00:03:49,400 So when I assign, effectively pass s by value into here, 75 00:03:50,190 --> 00:03:55,007 sparam, it'll move the value into sparam. 76 00:03:56,458 --> 00:04:00,273 So sparam, if I draw sparam here, 77 00:04:02,010 --> 00:04:05,010 whatever pointer the original string has, 78 00:04:05,010 --> 00:04:08,820 sparam basically points to the same place. 79 00:04:08,820 --> 00:04:12,180 And this original string loses ownership. 80 00:04:12,180 --> 00:04:16,860 Ownership has been moved into the parameter 81 00:04:16,860 --> 00:04:21,120 and the original string s is no longer usable. 82 00:04:21,120 --> 00:04:23,823 We have moved the data away from s. 83 00:04:25,680 --> 00:04:27,990 So inside the function, inside here, 84 00:04:27,990 --> 00:04:31,563 obviously I can use sparam and it'll print out hello. 85 00:04:33,420 --> 00:04:36,270 But of course, the problem is at the end of the function, 86 00:04:38,130 --> 00:04:41,940 this local sparam goes out of scope. 87 00:04:41,940 --> 00:04:46,940 So it it's destroyed, it's drop function is called 88 00:04:46,950 --> 00:04:51,950 and the drop function for sparam will deallocate the buffer. 89 00:04:52,200 --> 00:04:54,630 So it's gone away. 90 00:04:54,630 --> 00:04:56,790 And then when we come back to the original function, 91 00:04:56,790 --> 00:04:58,980 we can't use s anymore. 92 00:04:58,980 --> 00:05:02,490 S is no longer the owner of the text. 93 00:05:02,490 --> 00:05:05,040 When you pass a non-copy type 94 00:05:05,040 --> 00:05:09,480 by value into a function, your variable is basically dead 95 00:05:09,480 --> 00:05:10,470 from then on. 96 00:05:10,470 --> 00:05:13,263 You've given the object away to here. 97 00:05:14,520 --> 00:05:17,760 All right, so obviously, passing objects, 98 00:05:17,760 --> 00:05:21,990 non-copy objects by value has profound consequences. 99 00:05:21,990 --> 00:05:24,270 Once you've passed the value to the function, 100 00:05:24,270 --> 00:05:28,740 you've lost it and the other function now owns it. 101 00:05:28,740 --> 00:05:30,390 So let's have a look at an example. 102 00:05:30,390 --> 00:05:32,220 So for this chapter, we're gonna look 103 00:05:32,220 --> 00:05:35,210 at this project, lesson08_functions. 104 00:05:36,360 --> 00:05:39,273 So here we are in rustdev, lesson08_functions. 105 00:05:40,290 --> 00:05:43,593 Gonna open that project in Visual Studio Code. 106 00:05:44,460 --> 00:05:46,743 Here we are in Visual Studio Code. 107 00:05:47,940 --> 00:05:49,893 In my main file, 108 00:05:50,970 --> 00:05:54,960 I'm going to uncomment the code for the first demo, 109 00:05:54,960 --> 00:05:56,253 demo_passing_values. 110 00:05:57,990 --> 00:05:59,400 Right. 111 00:05:59,400 --> 00:06:01,200 I'm gonna look at how to pass other types 112 00:06:01,200 --> 00:06:03,570 and how to return other types as well later on. 113 00:06:03,570 --> 00:06:04,770 But demo_passing_values. 114 00:06:07,380 --> 00:06:11,400 So I've got an integer and a string. 115 00:06:11,400 --> 00:06:14,070 I pass the integer into the function. 116 00:06:14,070 --> 00:06:17,370 When I pass the integer into the function, 117 00:06:17,370 --> 00:06:19,203 it'll copy the value into here. 118 00:06:20,040 --> 00:06:23,610 We can use the value here, and at the end of the function, 119 00:06:23,610 --> 00:06:26,370 this local variable gets popped off the stack 120 00:06:26,370 --> 00:06:29,280 but n is still usable afterwards, okay? 121 00:06:29,280 --> 00:06:32,280 Because n is a integer, the value is copied 122 00:06:32,280 --> 00:06:35,070 but the original variable, it lives on. 123 00:06:35,070 --> 00:06:37,120 So we can still use n here 124 00:06:38,130 --> 00:06:39,510 but it's different for the string obviously 125 00:06:39,510 --> 00:06:43,530 because with a string, string doesn't implement copy. 126 00:06:43,530 --> 00:06:47,160 So when I pass that by value, it moves ownership 127 00:06:47,160 --> 00:06:51,570 of the text into here and it can use it in here. 128 00:06:51,570 --> 00:06:53,130 But then sparam gets dropped 129 00:06:53,130 --> 00:06:55,710 at the end of the function and the text is gone. 130 00:06:55,710 --> 00:06:57,750 I can't use this object, 131 00:06:57,750 --> 00:07:00,030 I can't use that variable afterwards 132 00:07:00,030 --> 00:07:01,830 because I've moved ownership into here 133 00:07:01,830 --> 00:07:03,570 and it's gone at the end of the function. 134 00:07:03,570 --> 00:07:06,510 I can't use s afterwards. 135 00:07:06,510 --> 00:07:08,613 Let's see what happens if I try to. 136 00:07:10,080 --> 00:07:15,080 I'm gonna get a compile error and do gonna do a cargo check 137 00:07:15,810 --> 00:07:17,860 to see what the error message is gonna be 138 00:07:19,800 --> 00:07:21,180 because it's good to know what the errors are 139 00:07:21,180 --> 00:07:23,040 because you will, you know, these mistakes, 140 00:07:23,040 --> 00:07:25,050 you will make them in your code as well. 141 00:07:25,050 --> 00:07:28,110 So it's good to know what the error message is likely to be. 142 00:07:28,110 --> 00:07:30,213 Oh, I wish I hadn't done it now. 143 00:07:32,075 --> 00:07:33,090 It says, right, well, 144 00:07:33,090 --> 00:07:38,090 in the beginning, you declared this string type. 145 00:07:39,990 --> 00:07:43,410 String doesn't implement the copy trait. 146 00:07:43,410 --> 00:07:45,330 Well, okay, we do know that. 147 00:07:45,330 --> 00:07:47,970 I then moved ownership. 148 00:07:47,970 --> 00:07:50,700 The value of the string was moved at that point. 149 00:07:50,700 --> 00:07:53,010 So s is no longer the owner. 150 00:07:53,010 --> 00:07:55,293 We can't use s afterwards. 151 00:07:56,160 --> 00:08:01,160 So then when we tried to use the string here on line 11, 152 00:08:03,030 --> 00:08:08,030 and get in the error, it says did you want to clone it maybe 153 00:08:08,100 --> 00:08:09,780 so that you didn't lose ownership, 154 00:08:09,780 --> 00:08:11,970 you just kind of copied the text? 155 00:08:11,970 --> 00:08:13,410 Hmm, interesting suggestion. 156 00:08:13,410 --> 00:08:14,640 Maybe that is what I wanted 157 00:08:14,640 --> 00:08:16,770 but certainly it's not working as it stands. 158 00:08:16,770 --> 00:08:19,080 So let me remove that statement. 159 00:08:19,080 --> 00:08:22,170 I can't, having passed my string, 160 00:08:22,170 --> 00:08:24,300 by value into this function, 161 00:08:24,300 --> 00:08:27,690 I can't then use that variable afterwards. 162 00:08:27,690 --> 00:08:29,280 So I've commented that line out. 163 00:08:29,280 --> 00:08:32,523 So that should at least compile now and run. 164 00:08:38,220 --> 00:08:43,220 Okay, so in some_func, iparam is 42 165 00:08:44,610 --> 00:08:48,780 and in some_func, sparam is hello. 166 00:08:48,780 --> 00:08:53,280 At the end of the function, the string was dropped. 167 00:08:53,280 --> 00:08:57,360 We come back up into our main code here, we can use n. 168 00:08:57,360 --> 00:09:01,443 N is still 42, but we can't use the string. 169 00:09:02,760 --> 00:09:07,760 So far what we've seen is if you pass a non-copy type 170 00:09:09,000 --> 00:09:12,990 by value into a function, then you give the object away. 171 00:09:12,990 --> 00:09:16,038 You can't then use that variable afterwards. 172 00:09:16,038 --> 00:09:17,310 What we're gonna look at next 173 00:09:17,310 --> 00:09:20,190 is to overcome this difficulty. 174 00:09:20,190 --> 00:09:24,180 So rather than passing the value and passing ownership, 175 00:09:24,180 --> 00:09:26,610 what you can do instead is pass a reference. 176 00:09:26,610 --> 00:09:29,513 And that's what we're going to look at in the next section.