1 00:00:06,570 --> 00:00:07,403 - In this section, 2 00:00:07,403 --> 00:00:09,300 we're going to see how to return a reference 3 00:00:09,300 --> 00:00:10,440 from a function. 4 00:00:10,440 --> 00:00:11,580 So first of all, 5 00:00:11,580 --> 00:00:13,830 to declare that a function returns a reference, 6 00:00:13,830 --> 00:00:15,150 you use the syntax 7 00:00:15,150 --> 00:00:16,170 here's a function, 8 00:00:16,170 --> 00:00:19,200 it returns a reference to some type. 9 00:00:19,200 --> 00:00:22,440 Inside the function to actually return a reference, 10 00:00:22,440 --> 00:00:23,730 it looks like this. 11 00:00:23,730 --> 00:00:27,180 This function specifies it returns a reference 12 00:00:27,180 --> 00:00:28,350 to some type. 13 00:00:28,350 --> 00:00:29,400 So inside the function 14 00:00:29,400 --> 00:00:32,550 you return a reference to some value of that type. 15 00:00:32,550 --> 00:00:33,720 So quite straightforward, 16 00:00:33,720 --> 00:00:37,500 an ampersand to indicate that you are returning a reference, 17 00:00:37,500 --> 00:00:38,790 an ampersand to indicate 18 00:00:38,790 --> 00:00:41,103 that the type is a reference as well. 19 00:00:41,970 --> 00:00:44,490 Right what about receiving a reference? 20 00:00:44,490 --> 00:00:45,810 When you call a function 21 00:00:45,810 --> 00:00:47,340 and it returns a reference 22 00:00:47,340 --> 00:00:49,353 how can you assign that to a variable? 23 00:00:50,190 --> 00:00:53,250 So here is an example 24 00:00:53,250 --> 00:00:55,350 using either implicit typing 25 00:00:55,350 --> 00:00:56,970 or explicit typing. 26 00:00:56,970 --> 00:00:58,890 So imagine you have a function 27 00:00:58,890 --> 00:01:00,270 that returns a reference. 28 00:01:00,270 --> 00:01:03,180 You can just assign the return value to a variable 29 00:01:03,180 --> 00:01:05,160 and the compiler will implicitly type this 30 00:01:05,160 --> 00:01:08,430 as a reference depending on what type it is, okay? 31 00:01:08,430 --> 00:01:10,290 A reference to a string, for example. 32 00:01:10,290 --> 00:01:12,000 Or alternatively, if you prefer, 33 00:01:12,000 --> 00:01:13,740 you can use explicit typing, 34 00:01:13,740 --> 00:01:17,220 call the function store the value in R 35 00:01:17,220 --> 00:01:20,040 which is explicitly a reference to some type. 36 00:01:20,040 --> 00:01:21,634 Given the choice 37 00:01:21,634 --> 00:01:22,467 I would prefer implicit typing 38 00:01:22,467 --> 00:01:24,150 because it's more concise, 39 00:01:24,150 --> 00:01:25,230 but explicit typing. 40 00:01:25,230 --> 00:01:27,150 You might wanna try that initially a few times 41 00:01:27,150 --> 00:01:28,260 just to convince yourself 42 00:01:28,260 --> 00:01:30,210 that you know the syntax. 43 00:01:30,210 --> 00:01:33,600 Right, now lifetime management Rust is very careful. 44 00:01:33,600 --> 00:01:35,040 It doesn't allow you 45 00:01:35,040 --> 00:01:38,010 to refer to an object of a short lifetime. 46 00:01:38,010 --> 00:01:42,462 So if you have a reference to an object, okay? 47 00:01:42,462 --> 00:01:46,500 But that object might disappear before the reference does 48 00:01:46,500 --> 00:01:48,450 the compiler won't let that happen. 49 00:01:48,450 --> 00:01:51,150 It won't let you have a dangling reference 50 00:01:51,150 --> 00:01:53,656 because obviously that's very dangerous. 51 00:01:53,656 --> 00:01:56,220 So you can't return a reference 52 00:01:56,220 --> 00:01:58,740 to a local stack based object 53 00:01:58,740 --> 00:02:00,390 because at the end of the function, 54 00:02:00,390 --> 00:02:02,040 that local stack based object 55 00:02:02,040 --> 00:02:03,805 is gonna be dropped 56 00:02:03,805 --> 00:02:06,210 and you'll end up returning a reference to something 57 00:02:06,210 --> 00:02:07,920 that is no longer there. 58 00:02:07,920 --> 00:02:10,560 And Rust doesn't allow dangling references. 59 00:02:10,560 --> 00:02:11,820 So here's an example, 60 00:02:11,820 --> 00:02:14,260 this code here will give a compiler error 61 00:02:15,480 --> 00:02:19,590 so inside the function, I've declared S 62 00:02:19,590 --> 00:02:23,607 as a local string object and it contains "Hi". 63 00:02:24,810 --> 00:02:27,600 And then I attempt to return a reference to S. 64 00:02:27,600 --> 00:02:29,970 Imagine the compiler let you do that, 65 00:02:29,970 --> 00:02:34,920 you would return back a reference to that string object. 66 00:02:34,920 --> 00:02:35,940 The problem is 67 00:02:35,940 --> 00:02:37,080 that that local string object 68 00:02:37,080 --> 00:02:39,570 is gonna be popped off the stack at the end of the function. 69 00:02:39,570 --> 00:02:41,340 It's gonna disappear. 70 00:02:41,340 --> 00:02:42,630 And what you've just done 71 00:02:42,630 --> 00:02:45,030 is you've returned back to the main corner, 72 00:02:45,030 --> 00:02:46,290 you've returned a reference 73 00:02:46,290 --> 00:02:48,870 to an object that is no longer there 74 00:02:48,870 --> 00:02:50,490 and the compiler won't let you do it. 75 00:02:50,490 --> 00:02:54,483 You can't return a reference to a local stack based object. 76 00:02:55,620 --> 00:02:58,650 This example also gives me an error, 77 00:02:58,650 --> 00:03:00,030 not quite so obvious 78 00:03:00,030 --> 00:03:02,340 but still it will give me an error. 79 00:03:02,340 --> 00:03:05,040 In this case, I've got a parameter 80 00:03:05,040 --> 00:03:07,740 and that parameter is basically on the stack, okay? 81 00:03:07,740 --> 00:03:09,210 So imagine this string, 82 00:03:09,210 --> 00:03:11,160 I've passed the value into this function 83 00:03:11,160 --> 00:03:14,310 and let's say this string is here 84 00:03:14,310 --> 00:03:17,130 it is still a local variable, okay? 85 00:03:17,130 --> 00:03:19,410 And therefore we'll go out of scope 86 00:03:19,410 --> 00:03:21,450 and be destroyed at the end of the function. 87 00:03:21,450 --> 00:03:23,250 So same problem as before. 88 00:03:23,250 --> 00:03:27,450 So whether you declare a local variable by value 89 00:03:27,450 --> 00:03:29,790 or whether you declare a parameter by value 90 00:03:29,790 --> 00:03:33,030 it's still local and on the stack. 91 00:03:33,030 --> 00:03:35,430 So if you did try to return a reference 92 00:03:35,430 --> 00:03:37,290 if the compiler let you do that, 93 00:03:37,290 --> 00:03:40,140 you'd be returning a reference to a string object 94 00:03:40,140 --> 00:03:43,080 that's about to disappear at the end of the function. 95 00:03:43,080 --> 00:03:45,270 And that would be a dangling reference. 96 00:03:45,270 --> 00:03:47,130 And that is not allowed, 97 00:03:47,130 --> 00:03:51,270 compiler error at this point, right? 98 00:03:51,270 --> 00:03:53,430 So it's all about lifetime management, 99 00:03:53,430 --> 00:03:55,650 basically Rust always needs to know 100 00:03:55,650 --> 00:03:58,613 that what it's referring to will live long enough 101 00:03:58,613 --> 00:04:03,570 so we don't end up with a reference into a void space. 102 00:04:03,570 --> 00:04:06,540 So one approach is to actually create the object 103 00:04:06,540 --> 00:04:08,460 before you call the function 104 00:04:08,460 --> 00:04:11,100 and pass it by reference into the function. 105 00:04:11,100 --> 00:04:13,110 So the variable or the object 106 00:04:13,110 --> 00:04:15,750 actually lives in the scope of the caller. 107 00:04:15,750 --> 00:04:17,640 When the call function returns 108 00:04:17,640 --> 00:04:20,190 the caller function still exists 109 00:04:20,190 --> 00:04:23,040 and its local variables are still in scope. 110 00:04:23,040 --> 00:04:26,640 So let's see an example of this. 111 00:04:26,640 --> 00:04:27,960 So from the top, 112 00:04:27,960 --> 00:04:30,330 I have a string S, 113 00:04:30,330 --> 00:04:32,820 that's my string object up here, 114 00:04:32,820 --> 00:04:34,287 and it contains "Hi". 115 00:04:35,280 --> 00:04:36,990 So far, so good. 116 00:04:36,990 --> 00:04:41,013 I pass a reference to that string object into here. 117 00:04:42,120 --> 00:04:45,280 So this here is a reference 118 00:04:47,460 --> 00:04:48,893 to that string, okay? 119 00:04:50,880 --> 00:04:53,070 And then because it's a reference, 120 00:04:53,070 --> 00:04:56,910 it is a reference I return S is a reference, isn't it? 121 00:04:56,910 --> 00:05:00,510 Yeah so this here is a string reference as well. 122 00:05:00,510 --> 00:05:01,860 So what I'll be returning 123 00:05:01,860 --> 00:05:04,500 is basically a reference to the same string object. 124 00:05:04,500 --> 00:05:07,090 I'll be returning back a reference 125 00:05:08,070 --> 00:05:09,720 to that string object. 126 00:05:09,720 --> 00:05:11,140 At the end of this function 127 00:05:12,270 --> 00:05:14,310 this string isn't destroyed 128 00:05:14,310 --> 00:05:15,810 because this was just borrow. 129 00:05:15,810 --> 00:05:17,160 We haven't got ownership of the string. 130 00:05:17,160 --> 00:05:20,070 We were just borrowing it temporarily. 131 00:05:20,070 --> 00:05:22,113 So the string object isn't destroyed. 132 00:05:23,010 --> 00:05:25,410 And the reference that gets returned back 133 00:05:25,410 --> 00:05:27,030 we put into R 134 00:05:27,030 --> 00:05:30,630 and R, refers to an object that still exists. 135 00:05:30,630 --> 00:05:32,230 Because basically the issue here 136 00:05:34,788 --> 00:05:36,600 is that the underlying string object 137 00:05:36,600 --> 00:05:39,630 was basically defined in this scope, okay? 138 00:05:39,630 --> 00:05:41,850 So we can borrow it here, 139 00:05:41,850 --> 00:05:43,560 we can return a reference to it here 140 00:05:43,560 --> 00:05:47,790 but it's this scope that basically owns the string. 141 00:05:47,790 --> 00:05:49,920 So you're allowed to return a reference to that string 142 00:05:49,920 --> 00:05:51,320 because it's still in scope. 143 00:05:52,470 --> 00:05:54,210 There's one last thing we need to think about 144 00:05:54,210 --> 00:05:55,590 and it's a bit complicated. 145 00:05:55,590 --> 00:05:58,880 Imagine you wanted to return a string literal. 146 00:05:58,880 --> 00:06:01,180 So imagine inside here 147 00:06:02,763 --> 00:06:06,607 I said we return "Hi" like that 148 00:06:08,580 --> 00:06:11,010 or something along those lines. 149 00:06:11,010 --> 00:06:12,240 Now, unfortunately 150 00:06:12,240 --> 00:06:15,240 although that might look quite straightforward 151 00:06:15,240 --> 00:06:16,260 it doesn't work. 152 00:06:16,260 --> 00:06:18,510 This function doesn't compile 153 00:06:18,510 --> 00:06:21,180 basically the problem is the compiler, 154 00:06:21,180 --> 00:06:23,640 basically remember what a string slice is, 155 00:06:23,640 --> 00:06:27,873 it's basically a pointer to some text somewhere 156 00:06:29,970 --> 00:06:33,090 and also the size of that slice 157 00:06:33,090 --> 00:06:34,860 it's a fat pointer like that. 158 00:06:34,860 --> 00:06:38,670 So the problem is, if you return a reference 159 00:06:38,670 --> 00:06:40,680 a string slice like that, 160 00:06:40,680 --> 00:06:43,080 the compiler can't be sure 161 00:06:43,080 --> 00:06:46,560 that this string text will live long enough. 162 00:06:46,560 --> 00:06:49,230 Imagine that text was in a local if statement 163 00:06:49,230 --> 00:06:51,840 it might be destroyed prematurely, 164 00:06:51,840 --> 00:06:54,060 in which case you've now got a slice 165 00:06:54,060 --> 00:06:56,790 that refers to text that is no longer there. 166 00:06:56,790 --> 00:06:59,280 The compiler must be absolutely sure 167 00:06:59,280 --> 00:07:01,890 about the lifetime of that text 168 00:07:01,890 --> 00:07:04,050 so that you can refer to it safely. 169 00:07:04,050 --> 00:07:05,340 So a common approach 170 00:07:05,340 --> 00:07:09,090 is to specify something called a static lifetime parameter. 171 00:07:09,090 --> 00:07:11,718 What you do is you put this syntax 172 00:07:11,718 --> 00:07:14,910 like an apostrophe static. 173 00:07:14,910 --> 00:07:16,800 It's called a lifetime parameter. 174 00:07:16,800 --> 00:07:18,150 And static lifetime 175 00:07:18,150 --> 00:07:19,320 means basically 176 00:07:19,320 --> 00:07:21,733 you're referring to an actual string literal. 177 00:07:21,733 --> 00:07:22,830 A string literal, 178 00:07:22,830 --> 00:07:25,050 remember, something you might declare 179 00:07:25,050 --> 00:07:27,420 as a literal string like that, 180 00:07:27,420 --> 00:07:31,080 is permanently allocated in the static storage 181 00:07:31,080 --> 00:07:32,430 of your application. 182 00:07:32,430 --> 00:07:34,020 So it'll always be there. 183 00:07:34,020 --> 00:07:37,710 So if you declare a string slice like this 184 00:07:37,710 --> 00:07:38,880 what you're telling the compiler 185 00:07:38,880 --> 00:07:41,400 is that you're basically gonna be referring to a text 186 00:07:41,400 --> 00:07:44,220 that will always be statically allocated. 187 00:07:44,220 --> 00:07:46,680 It's safe to refer to that string 188 00:07:46,680 --> 00:07:48,180 because it's always gonna be there 189 00:07:48,180 --> 00:07:50,133 because it has a static lifetime. 190 00:07:51,541 --> 00:07:54,050 And actually you'll see that cropping up 191 00:07:54,050 --> 00:07:55,770 in Rust code quite a lot as well. 192 00:07:55,770 --> 00:07:57,330 So I think we'll have a look at an example 193 00:07:57,330 --> 00:07:58,170 of all these things. 194 00:07:58,170 --> 00:08:00,750 It's lesson eight functions is a project 195 00:08:00,750 --> 00:08:02,850 demo return in reference. 196 00:08:02,850 --> 00:08:04,320 That's the code we're gonna look at 197 00:08:04,320 --> 00:08:05,280 and we're gonna run it. 198 00:08:05,280 --> 00:08:06,180 Let's take a look. 199 00:08:07,050 --> 00:08:10,205 Okay, so here we are on the code demo. 200 00:08:10,205 --> 00:08:12,270 First of all, let's look at the main code. 201 00:08:12,270 --> 00:08:15,600 We'll uncomment the demo that we are interested in 202 00:08:15,600 --> 00:08:18,300 demo return and reference this here. 203 00:08:18,300 --> 00:08:20,580 So I've got a couple of functions 204 00:08:20,580 --> 00:08:23,010 which are kind of well-written, 205 00:08:23,010 --> 00:08:24,300 and then I've got a couple of functions here 206 00:08:24,300 --> 00:08:26,070 that would give me some compiler errors. 207 00:08:26,070 --> 00:08:30,930 So we'll concentrate on the good functions first, okay? 208 00:08:30,930 --> 00:08:33,000 And I think I'll run it, 209 00:08:33,000 --> 00:08:35,520 and then we can kind of step through the results 210 00:08:35,520 --> 00:08:36,820 as we go through the code. 211 00:08:38,370 --> 00:08:40,357 So cargo run. 212 00:08:45,338 --> 00:08:48,450 Okay then, so from the top in demo we turn in reference. 213 00:08:48,450 --> 00:08:49,283 Do it, 214 00:08:49,283 --> 00:08:50,116 Yes, indeed. 215 00:08:50,116 --> 00:08:51,360 Here we are. 216 00:08:51,360 --> 00:08:53,880 So I create a string object, "Hello World" 217 00:08:53,880 --> 00:08:58,140 and I pass a reference of that string into this function. 218 00:08:58,140 --> 00:08:59,640 Okay now, 219 00:08:59,640 --> 00:09:02,910 if you have a function that takes a string slice 220 00:09:02,910 --> 00:09:06,240 you can always pass an actual string reference into it. 221 00:09:06,240 --> 00:09:08,610 So there's an automatic conversion. 222 00:09:08,610 --> 00:09:10,650 I'll just delete that comment for the moment. 223 00:09:10,650 --> 00:09:14,681 I pass in a string reference into a function 224 00:09:14,681 --> 00:09:18,780 that was expecting to receive a string slice. 225 00:09:18,780 --> 00:09:19,920 That's always allowed. 226 00:09:19,920 --> 00:09:22,500 You can take a reference to a whole string object 227 00:09:22,500 --> 00:09:24,030 and pass it into a slice. 228 00:09:24,030 --> 00:09:27,750 It basically points to the text inside the string. 229 00:09:27,750 --> 00:09:31,230 Okay, let me just reinstate that comment for later. 230 00:09:31,230 --> 00:09:34,380 So this basically receives effectively the text 231 00:09:34,380 --> 00:09:36,540 of the string, "Hello World". 232 00:09:36,540 --> 00:09:37,410 Then what I do 233 00:09:37,410 --> 00:09:39,720 is I iterate through all the characters 234 00:09:39,720 --> 00:09:41,640 looking for a space. 235 00:09:41,640 --> 00:09:44,460 As soon as I find a space, I stop. 236 00:09:44,460 --> 00:09:45,390 If it's not a space, 237 00:09:45,390 --> 00:09:47,490 I just increment the pause counter, 238 00:09:47,490 --> 00:09:51,630 pause will tell me the position of the first space. 239 00:09:51,630 --> 00:09:54,900 And then what I do is I return a string slice. 240 00:09:54,900 --> 00:09:57,030 I take my string, my string slice, 241 00:09:57,030 --> 00:09:58,920 the original string slice, 242 00:09:58,920 --> 00:10:03,180 and I get the characters from zero 243 00:10:03,180 --> 00:10:06,600 up to but not including the position of the space. 244 00:10:06,600 --> 00:10:09,210 So basically it'll be everything up to 245 00:10:09,210 --> 00:10:11,613 but not including the space character. 246 00:10:11,613 --> 00:10:15,411 It'll be a slice that basically references that part. 247 00:10:15,411 --> 00:10:19,785 All right, so that's a string slice that I return. 248 00:10:19,785 --> 00:10:22,230 I return a slice into the original string. 249 00:10:22,230 --> 00:10:27,207 R1 is a string slice that basically refers to "Hello". 250 00:10:28,140 --> 00:10:31,707 So when I print R1, it prints "Hello". 251 00:10:34,530 --> 00:10:38,220 Okay, and then I do the same thing again 252 00:10:38,220 --> 00:10:39,660 but this time 253 00:10:39,660 --> 00:10:42,000 I declare my reference variable 254 00:10:42,000 --> 00:10:44,580 as explicitly as a string reference 255 00:10:44,580 --> 00:10:46,860 as opposed to using type inference. 256 00:10:46,860 --> 00:10:48,750 I actually type it explicitly, 257 00:10:48,750 --> 00:10:51,750 but apart from that the example is exactly the same 258 00:10:51,750 --> 00:10:54,957 and therefore it also prints, "Hello". 259 00:10:56,250 --> 00:10:57,480 Very good. 260 00:10:57,480 --> 00:10:59,340 What about this get message function? 261 00:10:59,340 --> 00:11:01,860 Get message, I pass in my exam mark 262 00:11:01,860 --> 00:11:05,386 and it returns with back a string slice. 263 00:11:05,386 --> 00:11:08,700 So here's my get message function. 264 00:11:08,700 --> 00:11:11,100 I pass in my exam mark 265 00:11:11,100 --> 00:11:13,380 and it returns a string literal. 266 00:11:13,380 --> 00:11:15,540 If my mark is greater than it would go to 50, 267 00:11:15,540 --> 00:11:18,300 it returns this string literal, okay? 268 00:11:18,300 --> 00:11:21,810 Remember, a string literal in Rust 269 00:11:21,810 --> 00:11:24,150 is a string slice 270 00:11:24,150 --> 00:11:26,400 and you've got tell it 271 00:11:26,400 --> 00:11:30,600 that the slice is referring to statically allocated text. 272 00:11:30,600 --> 00:11:34,050 If we didn't put the dash static in there 273 00:11:34,050 --> 00:11:35,700 we'd get a compiler error. 274 00:11:35,700 --> 00:11:38,160 It would say I'm returning a reference to a string 275 00:11:38,160 --> 00:11:40,260 but I don't know the lifetime of that text, 276 00:11:40,260 --> 00:11:43,680 It could disappear underneath my feet, help. 277 00:11:43,680 --> 00:11:44,970 So we help. 278 00:11:44,970 --> 00:11:49,083 And return in a reference to statically allocated text. 279 00:11:49,980 --> 00:11:53,070 Okay So in other words, it'll be a pointer effectively 280 00:11:53,070 --> 00:11:55,080 to text which will always be there 281 00:11:55,080 --> 00:11:56,730 such as a string literal, 282 00:11:56,730 --> 00:12:00,120 you either pass or sadly you fail. 283 00:12:00,120 --> 00:12:02,760 Well, the marker passed then was 99. 284 00:12:02,760 --> 00:12:05,130 So the string literal that it returns, 285 00:12:05,130 --> 00:12:06,120 or the string slice 286 00:12:06,120 --> 00:12:08,040 basically it returns a string slice 287 00:12:08,040 --> 00:12:10,830 that points to that text pass. 288 00:12:10,830 --> 00:12:14,010 And that's what we return back up here. 289 00:12:14,010 --> 00:12:15,570 So the get message function 290 00:12:15,570 --> 00:12:20,250 returns a string slice which is pass. 291 00:12:20,250 --> 00:12:21,180 The message is pass. 292 00:12:21,180 --> 00:12:22,860 And again obviously here 293 00:12:22,860 --> 00:12:24,840 I didn't need to do explicit typing. 294 00:12:24,840 --> 00:12:26,740 I could have just used type inference. 295 00:12:27,660 --> 00:12:31,560 It depends whether you prefer explicit or implicit typing. 296 00:12:31,560 --> 00:12:34,173 Okay, couple of functions that won't work. 297 00:12:35,190 --> 00:12:38,760 This bad function number one won't work 298 00:12:38,760 --> 00:12:40,330 because I've got a local string 299 00:12:41,430 --> 00:12:44,850 and I'm trying to return a reference to a local string 300 00:12:44,850 --> 00:12:48,120 a dangling reference that would give me a compiler error. 301 00:12:48,120 --> 00:12:51,120 Let's, and comment that code 302 00:12:51,120 --> 00:12:54,270 and see what happens when I do a cargo check. 303 00:12:54,270 --> 00:12:56,943 So I'm expecting an error on line 35. 304 00:12:59,160 --> 00:13:01,380 Okay oh, actually it didn't get that far 305 00:13:01,380 --> 00:13:02,370 with a string slice. 306 00:13:02,370 --> 00:13:04,470 It needed a lifetime parameter, 307 00:13:04,470 --> 00:13:06,210 like static, for example. 308 00:13:06,210 --> 00:13:08,280 It needs to know the lifetime of the text 309 00:13:08,280 --> 00:13:09,150 that it was referring to. 310 00:13:09,150 --> 00:13:10,440 It even suggested 311 00:13:10,440 --> 00:13:13,590 that I give it a static storage lifetime specifier 312 00:13:13,590 --> 00:13:14,850 so it knows the length 313 00:13:14,850 --> 00:13:15,780 or the duration 314 00:13:15,780 --> 00:13:18,633 or the lifetime of the text that's gonna be returned. 315 00:13:19,759 --> 00:13:21,990 So that didn't work, did it? 316 00:13:21,990 --> 00:13:23,340 That was a bad function. 317 00:13:23,340 --> 00:13:25,273 And here's another bad function. 318 00:13:25,273 --> 00:13:27,540 So similar kind of reason, 319 00:13:27,540 --> 00:13:29,700 but a different actual example. 320 00:13:29,700 --> 00:13:31,560 I've got a local string here 321 00:13:31,560 --> 00:13:33,540 a string that was passed by value. 322 00:13:33,540 --> 00:13:34,890 This string S, 323 00:13:34,890 --> 00:13:37,320 is gonna disappear at the end of the function 324 00:13:37,320 --> 00:13:39,870 so it would be very dangerous 325 00:13:39,870 --> 00:13:41,730 to return a reference into that string 326 00:13:41,730 --> 00:13:43,770 because it's about to disappear. 327 00:13:43,770 --> 00:13:46,570 So this also is gonna give me a compiler error 328 00:13:49,046 --> 00:13:51,600 and it says same kind of thing really, 329 00:13:51,600 --> 00:13:52,620 on line 41, 330 00:13:52,620 --> 00:13:54,120 it really doesn't like the fact 331 00:13:54,120 --> 00:13:55,650 that this string size 332 00:13:55,650 --> 00:13:58,320 doesn't tell me how long the text is gonna live. 333 00:13:58,320 --> 00:14:00,300 I'm expecting some kind of lifetime parameter 334 00:14:00,300 --> 00:14:02,130 to tell me how long the object's gonna live. 335 00:14:02,130 --> 00:14:04,050 Otherwise, I'm not gonna let you return a reference to it 336 00:14:04,050 --> 00:14:06,060 in case it's a short lifetime. 337 00:14:06,060 --> 00:14:09,183 So it suggests maybe adding in a static specifier. 338 00:14:10,830 --> 00:14:14,610 Yeah, so basically the real solution for us in this case 339 00:14:14,610 --> 00:14:16,740 would've be to pass in a string reference. 340 00:14:16,740 --> 00:14:19,170 So instead of copying the original string 341 00:14:19,170 --> 00:14:21,420 and having a string that's gonna go up scope, 342 00:14:22,548 --> 00:14:24,660 we pass in a reference to the outer scope string 343 00:14:24,660 --> 00:14:28,380 whose lifetime is dictated by the caller 344 00:14:28,380 --> 00:14:31,290 which is gonna outlive the return value from this function. 345 00:14:31,290 --> 00:14:33,660 So you have to be careful, basically 346 00:14:33,660 --> 00:14:35,280 when you're returning references, 347 00:14:35,280 --> 00:14:38,070 you have to make sure that you return a reference 348 00:14:38,070 --> 00:14:40,560 to something whose lifetime is long enough 349 00:14:40,560 --> 00:14:42,003 for you to refer to. 350 00:14:42,866 --> 00:14:44,733 Okay so be careful.