1 00:00:06,540 --> 00:00:07,950 - Wow. So that's a lot we've covered. 2 00:00:07,950 --> 00:00:10,380 Let's have an example and then we'll run the example 3 00:00:10,380 --> 00:00:12,330 to see how it all hands together. 4 00:00:12,330 --> 00:00:14,373 So let's do that. 5 00:00:15,240 --> 00:00:18,960 Here we are then in our source code in main. 6 00:00:18,960 --> 00:00:21,180 We're going to look at this demo here, 7 00:00:21,180 --> 00:00:24,210 demo passing references. 8 00:00:24,210 --> 00:00:27,900 And it's here I've got a function. 9 00:00:27,900 --> 00:00:32,900 That function one, function one takes a string reference 10 00:00:33,810 --> 00:00:36,840 and an integer reference. 11 00:00:36,840 --> 00:00:40,530 And it uses the star to dereference 12 00:00:40,530 --> 00:00:43,290 to give me back the integer that I first thought of. 13 00:00:43,290 --> 00:00:45,240 It's like a pointer, really. 14 00:00:45,240 --> 00:00:46,140 Give me the contents 15 00:00:46,140 --> 00:00:48,990 of the integer, the second exam mark. 16 00:00:48,990 --> 00:00:52,320 And if the value is greater than 50, then it prints out. 17 00:00:52,320 --> 00:00:54,120 It'll print out the contents 18 00:00:54,120 --> 00:00:58,920 of the integer and it'll print out upon the string. 19 00:00:58,920 --> 00:01:00,870 It'll grab the contents of the string reference, 20 00:01:00,870 --> 00:01:02,460 the contents of the string, 21 00:01:02,460 --> 00:01:06,090 convert the string to uppercase, and then display it like so 22 00:01:06,090 --> 00:01:08,430 with a smiley face to indicate 23 00:01:08,430 --> 00:01:11,880 that you passed the exam because you got more than 50%. 24 00:01:11,880 --> 00:01:15,150 Or if the contents of the integer is less than 50 25 00:01:15,150 --> 00:01:19,950 or then it'll say, sadly you failed with a sad face. 26 00:01:19,950 --> 00:01:21,780 And again, it uses the star 27 00:01:21,780 --> 00:01:24,330 to dereference the integer reference 28 00:01:24,330 --> 00:01:26,190 and to dereference the string reference. 29 00:01:26,190 --> 00:01:30,630 And as I said, when you're invoking a method on an object 30 00:01:30,630 --> 00:01:33,390 if you have a reference, you can write it that way 31 00:01:33,390 --> 00:01:35,880 or you can just write it this way. 32 00:01:35,880 --> 00:01:38,490 You can just say reference.method 33 00:01:38,490 --> 00:01:40,050 and that would also work as well. 34 00:01:40,050 --> 00:01:42,210 So in practice, that is what you would do. 35 00:01:42,210 --> 00:01:45,270 You wouldn't bother dereferencing it 36 00:01:45,270 --> 00:01:47,310 if it's gonna happen automatically. 37 00:01:47,310 --> 00:01:49,590 Rust has special dispensation. 38 00:01:49,590 --> 00:01:51,330 When it sees a reference 39 00:01:51,330 --> 00:01:53,280 and then a method being invoked upon it, 40 00:01:53,280 --> 00:01:55,740 it'll automatically give you the object 41 00:01:55,740 --> 00:01:58,233 to dereference and invoked like so. 42 00:01:59,100 --> 00:02:03,390 So this first function takes a string reference. 43 00:02:03,390 --> 00:02:06,363 I can only pass in an actual string object into here. 44 00:02:08,110 --> 00:02:13,110 Okay, so up here in my main code when I call some func1. 45 00:02:13,170 --> 00:02:17,490 I give it a reference to n, my integer, 42. 46 00:02:17,490 --> 00:02:19,290 That's gonna fail unfortunately. 47 00:02:19,290 --> 00:02:21,510 I've got a string object here. 48 00:02:21,510 --> 00:02:24,150 I can pass a reference to the string. 49 00:02:24,150 --> 00:02:24,983 That'll work. 50 00:02:24,983 --> 00:02:27,003 But I can't pass in a string literal, 51 00:02:27,990 --> 00:02:32,370 as I was saying, when you have a string reference 52 00:02:32,370 --> 00:02:36,210 you can only pass in a string object reference. 53 00:02:36,210 --> 00:02:38,280 You can't pass in a string literal. 54 00:02:38,280 --> 00:02:40,080 That would give me a compiler error, 55 00:02:40,980 --> 00:02:42,930 which would be a shame. 56 00:02:42,930 --> 00:02:45,490 So I've got an improved version, some func2 57 00:02:46,500 --> 00:02:47,820 The only difference is that 58 00:02:47,820 --> 00:02:52,820 this one takes in a string slice, okay? 59 00:02:53,160 --> 00:02:55,860 The rest of the function is the same as before, 60 00:02:55,860 --> 00:02:57,690 except that it takes a string slice. 61 00:02:57,690 --> 00:02:59,310 And that means they can take either a reference 62 00:02:59,310 --> 00:03:03,690 to an actual string or just a string literal. 63 00:03:03,690 --> 00:03:08,160 So when I call some func2 I can call it with a reference 64 00:03:08,160 --> 00:03:13,160 to a string object, or I can pass in a string slice. 65 00:03:13,320 --> 00:03:18,150 Okay, so a string reference decays can be converted 66 00:03:18,150 --> 00:03:20,550 into a string slice. 67 00:03:20,550 --> 00:03:22,830 So you can pass in a string reference 68 00:03:22,830 --> 00:03:27,830 or just a slice, string text like so. 69 00:03:27,990 --> 00:03:31,170 Okay and that would work. 70 00:03:31,170 --> 00:03:32,313 And then finally, 71 00:03:33,270 --> 00:03:36,123 my third function, some func3. 72 00:03:37,210 --> 00:03:38,610 Here it is. 73 00:03:38,610 --> 00:03:41,850 I pass in a reference to an integer 74 00:03:41,850 --> 00:03:46,080 and a reference to a string, string slice, I should say. 75 00:03:46,080 --> 00:03:50,190 So prompter zero is my integer reference. 76 00:03:50,190 --> 00:03:53,849 I print that using the default formatter. 77 00:03:53,849 --> 00:03:57,870 And I also print prompter one using the default formatter. 78 00:03:57,870 --> 00:03:59,370 Okay, so this will print, 79 00:03:59,370 --> 00:04:01,200 the default formatter will print the value 80 00:04:01,200 --> 00:04:04,410 of the integer and the value of the string. 81 00:04:04,410 --> 00:04:08,190 And then I print prompter zero and prompter one again 82 00:04:08,190 --> 00:04:11,100 but this time using the pointer formatter. 83 00:04:11,100 --> 00:04:13,380 So it'll print out string, sorry, 84 00:04:13,380 --> 00:04:17,223 integer and string the addresses off. 85 00:04:18,450 --> 00:04:20,673 Okay, so let's run the example. 86 00:04:26,220 --> 00:04:28,770 Okay, so just from the top 87 00:04:28,770 --> 00:04:33,770 I have an integer n that's a bad exam mark and Ola Nordmann. 88 00:04:34,170 --> 00:04:37,020 I can pass those by reference. 89 00:04:37,020 --> 00:04:42,020 And inside that function, it printed out the value 90 00:04:42,480 --> 00:04:46,560 and the person's name and the fact that they failed, sadly. 91 00:04:46,560 --> 00:04:50,730 In the second call I passed in a string reference 92 00:04:50,730 --> 00:04:52,863 or a string slice. 93 00:04:53,700 --> 00:04:57,090 Okay, so that was the example of the string reference 94 00:04:57,090 --> 00:04:59,253 and that's the example of the string slice. 95 00:05:00,570 --> 00:05:02,550 And then in the third example, 96 00:05:02,550 --> 00:05:05,790 I pass in either a string object by reference 97 00:05:05,790 --> 00:05:07,860 or a string slice. 98 00:05:07,860 --> 00:05:10,470 Okay, so it prints out the values 99 00:05:10,470 --> 00:05:12,660 using the default formatter 100 00:05:12,660 --> 00:05:15,690 and then the address is using the pointer formatter 101 00:05:15,690 --> 00:05:20,690 both for the first call to the function on line 14 above 102 00:05:21,000 --> 00:05:24,630 and then on line 15 using Per Nordmann. 103 00:05:24,630 --> 00:05:26,520 And then finally, just to remind you 104 00:05:26,520 --> 00:05:28,530 that when you're passing by reference 105 00:05:28,530 --> 00:05:31,290 you don't lose ownership of the underlying variables. 106 00:05:31,290 --> 00:05:34,470 So I can still use s at the end of all of this. 107 00:05:34,470 --> 00:05:36,690 I can still use s. 108 00:05:36,690 --> 00:05:39,330 Okay, so it prints out s here. 109 00:05:39,330 --> 00:05:40,163 There we are. 110 00:05:40,163 --> 00:05:41,250 So a lot to cover 111 00:05:41,250 --> 00:05:43,260 but it is very important that you know 112 00:05:43,260 --> 00:05:46,830 passing by reference is the main way 113 00:05:46,830 --> 00:05:48,420 that you avoid losing ownership 114 00:05:48,420 --> 00:05:51,003 of objects when you pass them into functions.