1 00:00:06,473 --> 00:00:08,070 - In the previous section, 2 00:00:08,070 --> 00:00:11,160 we showed how to return a structure by value. 3 00:00:11,160 --> 00:00:13,920 And the example we saw was a builder function, 4 00:00:13,920 --> 00:00:15,660 which created an employee, 5 00:00:15,660 --> 00:00:19,110 and returned it by value to the caller. 6 00:00:19,110 --> 00:00:20,520 And the caller then received 7 00:00:20,520 --> 00:00:22,050 ownership of that object, 8 00:00:22,050 --> 00:00:23,550 and that's quite a simple, 9 00:00:23,550 --> 00:00:25,080 straightforward technique. 10 00:00:25,080 --> 00:00:26,820 It's also possible to return a structure 11 00:00:26,820 --> 00:00:29,040 by reference from a function, 12 00:00:29,040 --> 00:00:31,230 and then the caller can use the reference 13 00:00:31,230 --> 00:00:32,430 that you return. 14 00:00:32,430 --> 00:00:34,770 The reference refers to an object. 15 00:00:34,770 --> 00:00:36,750 The caller can use the object 16 00:00:36,750 --> 00:00:38,621 that you refer to. 17 00:00:38,621 --> 00:00:39,454 Okay. 18 00:00:39,454 --> 00:00:41,160 You have to be a bit careful when you return 19 00:00:41,160 --> 00:00:43,873 a reference that you don't return 20 00:00:43,873 --> 00:00:45,270 a dangling reference. 21 00:00:45,270 --> 00:00:47,580 For example, if you have a a local object, 22 00:00:47,580 --> 00:00:50,790 A local stack-based object inside a function 23 00:00:50,790 --> 00:00:52,860 you can't return a reference to it 24 00:00:52,860 --> 00:00:54,210 because at the end of the function, 25 00:00:54,210 --> 00:00:55,890 it would disappear, 26 00:00:55,890 --> 00:00:57,480 and you'd end up returning a reference 27 00:00:57,480 --> 00:00:59,460 to something that doesn't exist anymore. 28 00:00:59,460 --> 00:01:02,700 Rust doesn't let you return dangling references 29 00:01:02,700 --> 00:01:05,262 because it would be dangerous. 30 00:01:05,262 --> 00:01:06,780 Right. 31 00:01:06,780 --> 00:01:09,300 So here's a common scenario. 32 00:01:09,300 --> 00:01:10,680 I've got some function, 33 00:01:10,680 --> 00:01:12,690 like a hypothetical function, 34 00:01:12,690 --> 00:01:15,930 and it receives a couple of objects by reference. 35 00:01:15,930 --> 00:01:18,150 Now I've called it down here. 36 00:01:18,150 --> 00:01:20,220 So let's start the example down here 37 00:01:20,220 --> 00:01:22,380 in the higher level function. 38 00:01:22,380 --> 00:01:24,210 So I create some objects. 39 00:01:24,210 --> 00:01:26,670 Let's say here's an object, 40 00:01:26,670 --> 00:01:27,873 and here's an object. 41 00:01:28,740 --> 00:01:29,700 Like so. 42 00:01:29,700 --> 00:01:31,170 And I pass those objects 43 00:01:31,170 --> 00:01:34,140 by reference into the function, okay. 44 00:01:34,140 --> 00:01:37,630 So this function receives a reference 45 00:01:38,490 --> 00:01:42,870 to that object and a reference to that object. 46 00:01:42,870 --> 00:01:44,460 Let's see. 47 00:01:44,460 --> 00:01:46,860 And inside my function up here, 48 00:01:46,860 --> 00:01:47,760 what I want to do is, 49 00:01:47,760 --> 00:01:49,470 I want to return a reference 50 00:01:49,470 --> 00:01:50,910 to one of those objects. 51 00:01:50,910 --> 00:01:51,900 The example I'm gonna show 52 00:01:51,900 --> 00:01:53,850 in a minute is a comparison. 53 00:01:53,850 --> 00:01:56,730 Which employee earns the most money? 54 00:01:56,730 --> 00:01:58,500 Whichever employee earns the most, 55 00:01:58,500 --> 00:01:59,760 that's the employee 56 00:01:59,760 --> 00:02:01,667 I'm going to return a reference to. 57 00:02:01,667 --> 00:02:02,500 Okay. 58 00:02:02,500 --> 00:02:03,900 So what the compiler, sorry. 59 00:02:03,900 --> 00:02:05,250 What your code could do 60 00:02:05,250 --> 00:02:06,660 is it could return a reference 61 00:02:06,660 --> 00:02:07,650 to maybe this object. 62 00:02:07,650 --> 00:02:11,130 Maybe this object earns the most salary, okay. 63 00:02:11,130 --> 00:02:12,390 So what you could do 64 00:02:12,390 --> 00:02:14,520 is you could return basically 65 00:02:14,520 --> 00:02:18,210 a reference to that object. 66 00:02:18,210 --> 00:02:21,660 You could return that back to the client code. 67 00:02:21,660 --> 00:02:26,490 So the client code receives that reference. 68 00:02:26,490 --> 00:02:29,040 The client code receives the reference 69 00:02:29,040 --> 00:02:30,120 that you specified. 70 00:02:30,120 --> 00:02:34,140 So it would return a reference to the object, 71 00:02:34,140 --> 00:02:35,310 to the employee that earns 72 00:02:35,310 --> 00:02:37,620 the higher salary like that. 73 00:02:37,620 --> 00:02:39,960 And maybe it then prints out 74 00:02:39,960 --> 00:02:42,420 the details for that employee. 75 00:02:42,420 --> 00:02:43,253 Okay. 76 00:02:43,253 --> 00:02:44,490 So that's the common scenario. 77 00:02:44,490 --> 00:02:47,130 We created the objects down here. 78 00:02:47,130 --> 00:02:50,103 We passed references up here. 79 00:02:51,120 --> 00:02:53,040 It returned the reference 80 00:02:53,040 --> 00:02:55,110 to one of those objects. 81 00:02:55,110 --> 00:02:57,180 The highest paid employee for example. 82 00:02:57,180 --> 00:02:58,950 And then we can use that reference 83 00:02:58,950 --> 00:03:02,790 to output details for that employee. 84 00:03:02,790 --> 00:03:05,670 So that's the scenario we're going to achieve. 85 00:03:05,670 --> 00:03:06,990 It's a little bit more complicated 86 00:03:06,990 --> 00:03:08,463 than you'd probably imagine. 87 00:03:09,480 --> 00:03:11,760 Here's a simplistic implementation, 88 00:03:11,760 --> 00:03:13,650 and it doesn't work. 89 00:03:13,650 --> 00:03:14,483 Okay. 90 00:03:14,483 --> 00:03:19,020 So it seems to syntax wise, it seems okay. 91 00:03:19,020 --> 00:03:23,700 Imagine that I've got some employee object here 92 00:03:23,700 --> 00:03:25,890 that E1 is a reference to. 93 00:03:25,890 --> 00:03:27,993 E1 is a reference to an employee. 94 00:03:29,010 --> 00:03:31,560 So let's say E1 is a reference 95 00:03:31,560 --> 00:03:33,840 to this employee object, 96 00:03:33,840 --> 00:03:35,910 and I've got another employee object. 97 00:03:35,910 --> 00:03:38,940 Now, I tell you what the problem's gonna be. 98 00:03:38,940 --> 00:03:41,340 These objects that I'm referring to 99 00:03:41,340 --> 00:03:42,333 with E1 and E2, 100 00:03:44,190 --> 00:03:46,830 they could have different lifetimes. 101 00:03:46,830 --> 00:03:49,920 Maybe this object externally 102 00:03:49,920 --> 00:03:52,080 was defined inside an if statement. 103 00:03:52,080 --> 00:03:54,300 So it has quite a short lifetime, 104 00:03:54,300 --> 00:03:56,580 and maybe this employee object 105 00:03:56,580 --> 00:03:59,490 was defined in the whole function. 106 00:03:59,490 --> 00:04:01,800 So it has a longer lifetime, okay? 107 00:04:01,800 --> 00:04:04,380 And it's the lifetime that's the problem. 108 00:04:04,380 --> 00:04:05,913 So in my code, 109 00:04:06,930 --> 00:04:09,720 if the employee one's salary 110 00:04:09,720 --> 00:04:11,700 is greater than employee two, 111 00:04:11,700 --> 00:04:13,980 what I'm trying to do is to return 112 00:04:13,980 --> 00:04:16,590 basically a reference to E1. 113 00:04:16,590 --> 00:04:17,423 Okay? 114 00:04:17,423 --> 00:04:18,300 So I'm hoping in this case, 115 00:04:18,300 --> 00:04:20,130 I'm trying to return basically 116 00:04:20,130 --> 00:04:23,460 a reference to that employee object. 117 00:04:23,460 --> 00:04:24,720 So the caller, 118 00:04:24,720 --> 00:04:27,630 the caller function would receive a reference 119 00:04:27,630 --> 00:04:29,490 to that object or but maybe, 120 00:04:29,490 --> 00:04:30,780 it's the other way around. 121 00:04:30,780 --> 00:04:34,260 Maybe the second employee has the highest salary 122 00:04:34,260 --> 00:04:35,280 in which case, 123 00:04:35,280 --> 00:04:38,010 I'd be returning a reference to employee two. 124 00:04:38,010 --> 00:04:40,140 So in that case, the call in function 125 00:04:40,140 --> 00:04:42,750 would then receive a reference 126 00:04:42,750 --> 00:04:45,687 to this employee object over here. 127 00:04:45,687 --> 00:04:49,230 And the problem is, when we come back down here 128 00:04:49,230 --> 00:04:50,760 in our call in code, 129 00:04:50,760 --> 00:04:52,260 the compiler needs to know 130 00:04:52,260 --> 00:04:55,140 how long is this reference valid for? 131 00:04:55,140 --> 00:04:57,780 Or to put it another way, what am I referring to? 132 00:04:57,780 --> 00:05:00,038 Am I referring to an object 133 00:05:00,038 --> 00:05:02,580 that has a relatively short lifetime 134 00:05:02,580 --> 00:05:04,380 or something which has a relatively 135 00:05:04,380 --> 00:05:05,733 long lifetime like that? 136 00:05:06,570 --> 00:05:08,850 And the simple fact of the matter is, 137 00:05:08,850 --> 00:05:10,500 by the time that you returned the reference 138 00:05:10,500 --> 00:05:12,360 back to your calling code, 139 00:05:12,360 --> 00:05:15,270 the compiler doesn't know what happened. 140 00:05:15,270 --> 00:05:17,280 The compiler doesn't know really, 141 00:05:17,280 --> 00:05:19,470 the lifetime of the object you're referring to. 142 00:05:19,470 --> 00:05:21,690 It doesn't know what you wrote up here. 143 00:05:21,690 --> 00:05:23,220 Maybe you wrote it the other way around, 144 00:05:23,220 --> 00:05:24,540 salary less than. 145 00:05:24,540 --> 00:05:25,770 So it doesn't know, 146 00:05:25,770 --> 00:05:28,950 which object lifetime to to go by. 147 00:05:28,950 --> 00:05:30,510 It doesn't know how long this reference 148 00:05:30,510 --> 00:05:32,220 is gonna be valid for 149 00:05:32,220 --> 00:05:33,390 because it doesn't know 150 00:05:33,390 --> 00:05:35,943 the lifetime of the object that you've returned. 151 00:05:37,440 --> 00:05:39,450 So we get a compiler error 152 00:05:39,450 --> 00:05:41,091 if you try to write the function 153 00:05:41,091 --> 00:05:42,780 in a simplistic way like this, 154 00:05:42,780 --> 00:05:45,900 even though it all seems legitimate 155 00:05:45,900 --> 00:05:47,250 it doesn't tell the compiler 156 00:05:47,250 --> 00:05:50,580 enough about the lifetime of the object 157 00:05:50,580 --> 00:05:52,430 that you're returning a reference to. 158 00:05:53,430 --> 00:05:55,110 The function attempts to return a reference 159 00:05:55,110 --> 00:05:58,353 to either E1 or E2, 160 00:05:59,220 --> 00:06:01,830 but they could have different lifetimes 161 00:06:01,830 --> 00:06:03,690 in which case the compiler doesn't know 162 00:06:03,690 --> 00:06:06,030 how long will the reference be valid for, 163 00:06:06,030 --> 00:06:08,730 for that long or for that long. 164 00:06:08,730 --> 00:06:11,160 And the compiler needs to know. 165 00:06:11,160 --> 00:06:13,290 The whole point of Rust is to avoid 166 00:06:13,290 --> 00:06:14,190 this kind of error, 167 00:06:14,190 --> 00:06:16,203 like dangling references effectively. 168 00:06:17,730 --> 00:06:20,340 So the solution is a new piece of syntax 169 00:06:20,340 --> 00:06:22,440 that we haven't seen before. 170 00:06:22,440 --> 00:06:23,730 What you've gotta do, 171 00:06:23,730 --> 00:06:25,470 is you've gotta say 172 00:06:25,470 --> 00:06:26,920 something about the lifetime. 173 00:06:27,810 --> 00:06:30,090 The syntax that I've shown here, 174 00:06:30,090 --> 00:06:35,090 this is called lifetime annotation. 175 00:06:35,160 --> 00:06:38,340 Inside angle brackets and that hyphen, 176 00:06:38,340 --> 00:06:40,560 you have some kind of lifetime specifier. 177 00:06:40,560 --> 00:06:42,000 I've called it A. 178 00:06:42,000 --> 00:06:43,440 I could have called it B. 179 00:06:43,440 --> 00:06:44,730 I could have called it Z. 180 00:06:44,730 --> 00:06:46,620 I've just called it A here. 181 00:06:46,620 --> 00:06:51,240 And it says there is some lifetime involved. 182 00:06:51,240 --> 00:06:53,610 And then what I've done is I've applied 183 00:06:53,610 --> 00:06:55,410 that lifetime specifier. 184 00:06:55,410 --> 00:06:57,810 I've applied this lifetime annotation 185 00:06:57,810 --> 00:07:01,503 to both of these parameters here. 186 00:07:02,430 --> 00:07:03,450 Okay. 187 00:07:03,450 --> 00:07:04,860 What it indicates, 188 00:07:04,860 --> 00:07:06,480 it indicates that they both have 189 00:07:06,480 --> 00:07:07,313 the same lifetime. 190 00:07:07,313 --> 00:07:10,500 Because I've used the same lifetime specifier A, 191 00:07:10,500 --> 00:07:12,783 it says whatever E1 refers to, 192 00:07:15,600 --> 00:07:17,160 it will have the same lifetime 193 00:07:17,160 --> 00:07:21,360 as whatever E2 refers to, okay? 194 00:07:21,360 --> 00:07:23,310 And furthermore, 195 00:07:23,310 --> 00:07:26,400 the return type is also annotated 196 00:07:26,400 --> 00:07:28,530 with the lifetime annotation. 197 00:07:28,530 --> 00:07:30,720 It means that whatever I return, 198 00:07:30,720 --> 00:07:33,090 the return type will be a reference 199 00:07:33,090 --> 00:07:37,620 to either E1 or a reference to E2, 200 00:07:37,620 --> 00:07:41,190 but they have the same lifetime. 201 00:07:41,190 --> 00:07:45,540 So in my main code, I'll receive a reference. 202 00:07:45,540 --> 00:07:47,700 It doesn't matter whether I receive a reference 203 00:07:47,700 --> 00:07:50,700 to E1 or E2 204 00:07:50,700 --> 00:07:54,273 because they have the same lifetime. 205 00:07:55,650 --> 00:07:58,530 So that's a tricky thing. 206 00:07:58,530 --> 00:07:59,550 That's something definitely 207 00:07:59,550 --> 00:08:01,653 that you wanna practice and get used to. 208 00:08:03,240 --> 00:08:06,390 By annotating both of these parameters 209 00:08:06,390 --> 00:08:08,160 with the same lifetime parameter, 210 00:08:08,160 --> 00:08:09,870 I'm indicating to the compiler, 211 00:08:09,870 --> 00:08:12,300 they have the same lifetime or rather, 212 00:08:12,300 --> 00:08:13,740 what I'm indicating is, 213 00:08:13,740 --> 00:08:15,120 when the function's called, 214 00:08:15,120 --> 00:08:16,710 it would better receive, 215 00:08:16,710 --> 00:08:19,020 it must receive references to objects 216 00:08:19,020 --> 00:08:20,700 that have the same lifetime. 217 00:08:20,700 --> 00:08:22,260 So it doesn't matter 218 00:08:22,260 --> 00:08:24,090 whether we return a reference to the first one 219 00:08:24,090 --> 00:08:24,923 or the second one 220 00:08:24,923 --> 00:08:26,790 because they have the same lifetime. 221 00:08:26,790 --> 00:08:28,950 The compiler can kind of envisage 222 00:08:28,950 --> 00:08:30,000 what's gonna happen 223 00:08:30,000 --> 00:08:31,700 when that reference gets returned. 224 00:08:33,270 --> 00:08:36,600 If you want to have a mutable reference return, 225 00:08:36,600 --> 00:08:39,420 it's just an extension of the syntax really. 226 00:08:39,420 --> 00:08:41,670 The key point here is that, 227 00:08:41,670 --> 00:08:44,940 the lifetime specifier is needed really 228 00:08:44,940 --> 00:08:48,690 because we need to indicate the lifetime 229 00:08:48,690 --> 00:08:51,900 of the thing being returned will be the same 230 00:08:51,900 --> 00:08:54,930 as the lifetime of the parameters coming in. 231 00:08:54,930 --> 00:08:57,243 If I explain that from the other direction, 232 00:08:58,290 --> 00:09:02,020 the first parameter, E1 is a reference 233 00:09:03,330 --> 00:09:05,133 to mutable object there, 234 00:09:06,150 --> 00:09:08,517 and it has a certain lifetime. 235 00:09:08,517 --> 00:09:11,130 And the second parameter is also a reference 236 00:09:11,130 --> 00:09:12,090 to a mutable parameter, 237 00:09:12,090 --> 00:09:13,860 and it has the same lifetime. 238 00:09:13,860 --> 00:09:16,683 And the returned reference is also mutable. 239 00:09:18,390 --> 00:09:19,223 Okay. 240 00:09:19,223 --> 00:09:21,180 So let's have a look at the example. 241 00:09:21,180 --> 00:09:23,970 My types .RS is our employee structure. 242 00:09:23,970 --> 00:09:26,160 Main runs the code. 243 00:09:26,160 --> 00:09:27,300 The interesting part is in 244 00:09:27,300 --> 00:09:29,613 demo_struct_return reference. 245 00:09:30,570 --> 00:09:31,710 Right. 246 00:09:31,710 --> 00:09:33,630 So main. 247 00:09:33,630 --> 00:09:36,870 Sorry, my types defines the structure. 248 00:09:36,870 --> 00:09:39,300 Main is going to run the demo. 249 00:09:39,300 --> 00:09:41,130 So let's run it there. 250 00:09:41,130 --> 00:09:42,773 And demo_struct_return_reference. 251 00:09:45,330 --> 00:09:46,800 So I've got a couple of functions, 252 00:09:46,800 --> 00:09:47,940 which are the same as before. 253 00:09:47,940 --> 00:09:50,130 So let me just explain those first. 254 00:09:50,130 --> 00:09:54,270 A build function that returns a new employee 255 00:09:54,270 --> 00:09:56,430 with the data on the stack. 256 00:09:56,430 --> 00:09:58,260 I've got a print employee, 257 00:09:58,260 --> 00:10:00,390 which just takes an employee by reference, 258 00:10:00,390 --> 00:10:01,950 and prints this details. 259 00:10:01,950 --> 00:10:04,890 So those two functions are the same as before. 260 00:10:04,890 --> 00:10:09,513 What's different is my choose employee function. 261 00:10:10,380 --> 00:10:13,590 It receives a reference to an employee, 262 00:10:13,590 --> 00:10:16,050 with a certain lifetime and a reference 263 00:10:16,050 --> 00:10:19,470 to another employee with the same lifetime 264 00:10:19,470 --> 00:10:21,300 as specified by this lifetime parameter, 265 00:10:21,300 --> 00:10:22,362 which I've called A, 266 00:10:22,362 --> 00:10:24,510 but I could have called anything. 267 00:10:24,510 --> 00:10:27,780 The reference that's returned will be a reference 268 00:10:27,780 --> 00:10:29,910 to an object with the same lifetime 269 00:10:29,910 --> 00:10:31,380 as these objects. 270 00:10:31,380 --> 00:10:32,213 Okay? 271 00:10:32,213 --> 00:10:33,690 So the compiler knows what to expect back. 272 00:10:33,690 --> 00:10:35,250 It'll receive back a reference 273 00:10:35,250 --> 00:10:37,320 to something that has the same lifetime 274 00:10:37,320 --> 00:10:38,583 as the thing's passed in. 275 00:10:40,050 --> 00:10:42,330 So I check the salary/ 276 00:10:42,330 --> 00:10:45,300 If employee one earns more than employee two, 277 00:10:45,300 --> 00:10:48,600 then turn a reference to employee one, 278 00:10:48,600 --> 00:10:50,600 else return a reference to employee two. 279 00:10:51,930 --> 00:10:54,600 Similar kind of idea for the mutable example, 280 00:10:54,600 --> 00:10:56,910 except here the parameters 281 00:10:56,910 --> 00:10:58,860 are mutable references 282 00:10:58,860 --> 00:11:01,890 to objects with a certain lifetime. 283 00:11:01,890 --> 00:11:03,840 Another mutable reference 284 00:11:03,840 --> 00:11:06,690 to an object with a certain lifetime. 285 00:11:06,690 --> 00:11:08,077 And it returns a reference, 286 00:11:08,077 --> 00:11:09,450 a mutable reference 287 00:11:09,450 --> 00:11:11,160 to an object with the same lifetime. 288 00:11:11,160 --> 00:11:13,680 So the returned employee reference 289 00:11:13,680 --> 00:11:15,090 will have the same lifetime 290 00:11:15,090 --> 00:11:16,890 as the two employees that were passed in. 291 00:11:16,890 --> 00:11:18,810 So the compiler can predict 292 00:11:18,810 --> 00:11:19,830 how long that reference 293 00:11:19,830 --> 00:11:21,600 that's returned will be valid for. 294 00:11:21,600 --> 00:11:22,980 The objects that were passed in 295 00:11:22,980 --> 00:11:24,480 will have a certain lifetime. 296 00:11:24,480 --> 00:11:26,670 I know that the returned reference 297 00:11:26,670 --> 00:11:28,380 will have that lifetime, 298 00:11:28,380 --> 00:11:29,850 so that the compiler can make sure 299 00:11:29,850 --> 00:11:32,463 that we only use the object while it's in scope. 300 00:11:33,450 --> 00:11:34,283 Okay? 301 00:11:34,283 --> 00:11:38,220 And then we just return either E1 or E2. 302 00:11:38,220 --> 00:11:39,630 In my main code, 303 00:11:39,630 --> 00:11:43,170 I create a couple of mutable employees. 304 00:11:43,170 --> 00:11:45,510 I receive back a mutable reference 305 00:11:45,510 --> 00:11:49,050 to either E1 or E2 and then I print 306 00:11:49,050 --> 00:11:50,850 whatever's being returned by reference. 307 00:11:50,850 --> 00:11:52,770 I've called it RI. 308 00:11:52,770 --> 00:11:53,603 Okay. 309 00:11:53,603 --> 00:11:56,790 Reference immutable was the name behind that. 310 00:11:56,790 --> 00:12:00,060 And then I've got another variable here, RM. 311 00:12:00,060 --> 00:12:01,590 Reference mutable. 312 00:12:01,590 --> 00:12:04,200 When I call the second function, 313 00:12:04,200 --> 00:12:05,790 I pass in an immutable reference 314 00:12:05,790 --> 00:12:07,320 to E1 and E1. 315 00:12:07,320 --> 00:12:08,820 I'll get back a immutable reference 316 00:12:08,820 --> 00:12:10,590 to E1 or E1. 317 00:12:10,590 --> 00:12:12,750 Give the person a handsome salary, 318 00:12:12,750 --> 00:12:14,613 and then print the person's details. 319 00:12:15,540 --> 00:12:16,533 I hope this works. 320 00:12:17,730 --> 00:12:18,563 Let's see. 321 00:12:20,790 --> 00:12:22,083 Cargo run. 322 00:12:24,884 --> 00:12:25,920 Okay. 323 00:12:25,920 --> 00:12:29,370 So the first employee earns more. 324 00:12:29,370 --> 00:12:31,533 So Jane. 325 00:12:32,580 --> 00:12:34,440 Jane earns 1,001, 326 00:12:34,440 --> 00:12:36,510 whereas John only earned a thousand. 327 00:12:36,510 --> 00:12:38,610 So comparing the salaries, 328 00:12:38,610 --> 00:12:39,870 Jane earns them more, 329 00:12:39,870 --> 00:12:41,280 so that the object that was returned 330 00:12:41,280 --> 00:12:44,043 by reference was Jane in both cases. 331 00:12:45,030 --> 00:12:46,380 Plus a double salary. 332 00:12:46,380 --> 00:12:49,320 So the key point then is, 333 00:12:49,320 --> 00:12:51,330 when you return a reference, 334 00:12:51,330 --> 00:12:53,430 the compiler needs to know 335 00:12:53,430 --> 00:12:56,340 how long does that object live for, 336 00:12:56,340 --> 00:12:58,673 so that I know that the reference is still valid. 337 00:12:58,673 --> 00:12:59,506 Okay. 338 00:12:59,506 --> 00:13:01,590 And by using this lifetime parameter, 339 00:13:01,590 --> 00:13:03,600 I've said, whatever object gets 340 00:13:03,600 --> 00:13:05,790 passed into the first parameter, 341 00:13:05,790 --> 00:13:07,440 will have that lifetime, 342 00:13:07,440 --> 00:13:09,300 and so will the second object. 343 00:13:09,300 --> 00:13:11,580 So the returned reference 344 00:13:11,580 --> 00:13:14,100 will be valid for the same amount of time 345 00:13:14,100 --> 00:13:16,500 as the incoming objects were valid. 346 00:13:16,500 --> 00:13:17,333 Okay. 347 00:13:17,333 --> 00:13:18,990 So I can use the returned reference. 348 00:13:18,990 --> 00:13:20,550 As long as these objects 349 00:13:20,550 --> 00:13:22,650 were passed in were valid, 350 00:13:22,650 --> 00:13:25,050 then the returned reference will still be valid, 351 00:13:25,050 --> 00:13:26,250 and the compiler can make sure 352 00:13:26,250 --> 00:13:27,360 that I've used the object, 353 00:13:27,360 --> 00:13:29,880 The reference correctly there. 354 00:13:29,880 --> 00:13:31,620 So you might want to do 355 00:13:31,620 --> 00:13:33,510 a little bit of reading around on this concept. 356 00:13:33,510 --> 00:13:35,610 Lifetime annotations. 357 00:13:35,610 --> 00:13:38,280 It is quite tricky, but it's to avoid 358 00:13:38,280 --> 00:13:39,750 some of the more insidious error 359 00:13:39,750 --> 00:13:42,660 that you can get in C++ and other languages 360 00:13:42,660 --> 00:13:44,010 where you have a reference 361 00:13:44,010 --> 00:13:45,900 to something that's no longer there, 362 00:13:45,900 --> 00:13:47,370 it just disappeared. 363 00:13:47,370 --> 00:13:50,010 Rust gives you that kind of a surety 364 00:13:50,010 --> 00:13:51,570 without having to rely on you know, 365 00:13:51,570 --> 00:13:53,580 complicated things like garbage collections, 366 00:13:53,580 --> 00:13:55,350 and reference company and so on. 367 00:13:55,350 --> 00:13:57,030 Okay, so there we go. 368 00:13:57,030 --> 00:13:58,533 Lifetime annotations.