1 00:00:06,540 --> 00:00:08,700 - In this section, we're going to look at how to pass 2 00:00:08,700 --> 00:00:10,710 multiple references into a function. 3 00:00:10,710 --> 00:00:12,930 First of all, a quick recap of how to pass 4 00:00:12,930 --> 00:00:15,090 immutable references into a function. 5 00:00:15,090 --> 00:00:17,250 Have a look at this example here. 6 00:00:17,250 --> 00:00:21,877 I have a function that takes an immutable reference to &i32 7 00:00:23,160 --> 00:00:26,040 and I pass in a reference there, I borrow it, 8 00:00:26,040 --> 00:00:28,920 and it takes an immutable reference to a string 9 00:00:28,920 --> 00:00:30,060 which I pass in here. 10 00:00:30,060 --> 00:00:31,980 I borrow the string in here. 11 00:00:31,980 --> 00:00:33,630 So inside that function, 12 00:00:33,630 --> 00:00:35,910 we can access the original contents, 13 00:00:35,910 --> 00:00:37,863 but we can't modify the values. 14 00:00:38,700 --> 00:00:41,850 Okay, so if you do want to modify a value, 15 00:00:41,850 --> 00:00:44,550 then you can pass in a mutable reference. 16 00:00:44,550 --> 00:00:46,290 Obviously, the object itself 17 00:00:46,290 --> 00:00:49,083 must originally be mutable to support this. 18 00:00:49,950 --> 00:00:52,470 So when you pass in a parameter, 19 00:00:52,470 --> 00:00:55,260 precede the parameter name with &mut 20 00:00:55,260 --> 00:00:57,420 as a multiple reference. 21 00:00:57,420 --> 00:00:58,860 Here is an example. 22 00:00:58,860 --> 00:01:00,510 I'm calling in a function 23 00:01:00,510 --> 00:01:04,660 and passing in a mutable reference to my integer n 24 00:01:05,550 --> 00:01:09,060 and I'm passing in a mutable reference to my string s. 25 00:01:09,060 --> 00:01:11,640 So the function will receive a reference, 26 00:01:11,640 --> 00:01:13,320 a mutable reference, 27 00:01:13,320 --> 00:01:16,713 that it can change to the integer and to the string. 28 00:01:18,300 --> 00:01:20,160 Right, well, inside the function, 29 00:01:20,160 --> 00:01:23,610 when you receive a mutable reference, remember in Rust, 30 00:01:23,610 --> 00:01:25,770 a reference is really like a pointer. 31 00:01:25,770 --> 00:01:28,800 So you have to use the star to dereference 32 00:01:28,800 --> 00:01:30,690 and get back to the original value, 33 00:01:30,690 --> 00:01:32,610 which you can then change. 34 00:01:32,610 --> 00:01:34,110 So here's a function. 35 00:01:34,110 --> 00:01:38,460 It receives a mutable reference to an integer. 36 00:01:38,460 --> 00:01:40,830 Okay, so basically it's like a pointer really 37 00:01:40,830 --> 00:01:43,770 that points to some integer up here 38 00:01:43,770 --> 00:01:46,083 and it receives and I can then change it here. 39 00:01:47,010 --> 00:01:51,960 I can use the star to basically access the contents 40 00:01:51,960 --> 00:01:55,950 of where the reference is pointing to and change that value. 41 00:01:55,950 --> 00:01:57,810 And in exactly the same kind of way 42 00:01:57,810 --> 00:02:00,750 for my string reference parameter, 43 00:02:00,750 --> 00:02:04,200 sparam is a mutable reference to a string. 44 00:02:04,200 --> 00:02:07,080 So it kinda points to some string object over here 45 00:02:07,080 --> 00:02:10,770 that contains some text, something like that. 46 00:02:10,770 --> 00:02:12,870 And then in order to actually change that text, 47 00:02:12,870 --> 00:02:17,160 we can dereference to get back to the original string 48 00:02:17,160 --> 00:02:19,260 and then we can change the value in the string. 49 00:02:19,260 --> 00:02:23,370 We can append World, for example, like that. 50 00:02:23,370 --> 00:02:27,030 Okay, so just one other thing to remember. 51 00:02:27,030 --> 00:02:29,940 When you're invoking a method on a reference, 52 00:02:29,940 --> 00:02:32,460 you can dereference explicitly 53 00:02:32,460 --> 00:02:34,560 to get back to the original object, 54 00:02:34,560 --> 00:02:37,800 but Rust also supports the shorthand syntax. 55 00:02:37,800 --> 00:02:39,330 It'll automatically, 56 00:02:39,330 --> 00:02:42,660 when you invoke a method on a reference, 57 00:02:42,660 --> 00:02:46,230 it'll automatically dereference as if you wrote it that way. 58 00:02:46,230 --> 00:02:48,180 So in practice, people always write it this way 59 00:02:48,180 --> 00:02:49,293 because it's easier. 60 00:02:51,540 --> 00:02:53,640 Right, well, let's see an example. 61 00:02:53,640 --> 00:02:57,180 In Lesson 8 functions, we'll have a look at the demo 62 00:02:57,180 --> 00:03:01,323 passing mutable references example and then we'll run it. 63 00:03:02,280 --> 00:03:06,520 Okay, so then in the main code, let me uncomment 64 00:03:08,089 --> 00:03:11,550 demo_passing_mutable_references, 65 00:03:11,550 --> 00:03:13,383 and here it is. 66 00:03:14,340 --> 00:03:17,490 So I have a function that receives a mutable reference 67 00:03:17,490 --> 00:03:20,913 to an integer and a mutable reference to a string. 68 00:03:21,870 --> 00:03:23,760 It outputs the values initially. 69 00:03:23,760 --> 00:03:26,760 Remember, when you have references, 70 00:03:26,760 --> 00:03:31,650 the default formatter automatically dereferences for you. 71 00:03:31,650 --> 00:03:34,020 So when you use the default formatter here, 72 00:03:34,020 --> 00:03:37,830 it's as if you put a star here 73 00:03:37,830 --> 00:03:39,750 except that you don't actually have to. 74 00:03:39,750 --> 00:03:42,900 It automatically dereferences for you 75 00:03:42,900 --> 00:03:46,230 when you use the default formatter, which is quite nice. 76 00:03:46,230 --> 00:03:47,970 And then we use the star 77 00:03:47,970 --> 00:03:49,950 to increment the value of the number. 78 00:03:49,950 --> 00:03:54,950 And I could have also used the star here for my method call, 79 00:03:54,990 --> 00:03:58,260 but I've chosen not to because it would work anyway. 80 00:03:58,260 --> 00:04:00,240 So whatever string I'm referring to 81 00:04:00,240 --> 00:04:02,190 will now have World appended 82 00:04:02,190 --> 00:04:04,350 and then it outputs the values after that. 83 00:04:04,350 --> 00:04:06,453 Let's run it and see what happens. 84 00:04:15,130 --> 00:04:19,380 Okay, so from the top, n is initially 42 85 00:04:19,380 --> 00:04:21,360 and the string is hello, 86 00:04:21,360 --> 00:04:23,520 pass in a mutable reference to n 87 00:04:23,520 --> 00:04:25,890 and a mutable reference to s. 88 00:04:25,890 --> 00:04:27,450 Okay, so the values can be changed. 89 00:04:27,450 --> 00:04:31,200 It outputs the initial values of n and s 90 00:04:31,200 --> 00:04:34,170 which are 42 and hello. 91 00:04:34,170 --> 00:04:37,920 It changes the values, it adds 10, and it adds a string, 92 00:04:37,920 --> 00:04:40,500 and then it prints the values after changing the number 93 00:04:40,500 --> 00:04:42,090 and after changing the string. 94 00:04:42,090 --> 00:04:45,483 It outputs the value afterward, 52, Hello World. 95 00:04:46,320 --> 00:04:48,720 When you refer to a string, 96 00:04:48,720 --> 00:04:51,480 it is the original string that you're updating. 97 00:04:51,480 --> 00:04:54,150 Okay, so it is the actual real string that's been changed 98 00:04:54,150 --> 00:04:56,610 and the real integer that's been changed. 99 00:04:56,610 --> 00:04:59,010 So when that function returns, 100 00:04:59,010 --> 00:05:00,810 it's the real values that have changed, 101 00:05:00,810 --> 00:05:03,870 n is 52 and the string is hello world. 102 00:05:03,870 --> 00:05:05,070 I change it again. 103 00:05:05,070 --> 00:05:07,020 We still own these values 104 00:05:07,020 --> 00:05:10,980 so I'm gonna add a million and some thumbs up 105 00:05:10,980 --> 00:05:13,830 and then I print the value of n and s afterwards. 106 00:05:13,830 --> 00:05:18,830 So afterwards, n is 1,000,052 and s is Hello World 107 00:05:20,550 --> 00:05:23,250 thumbs up, thumbs up, thumbs up. 108 00:05:23,250 --> 00:05:26,610 Okay, so that's actually quite straightforward. 109 00:05:26,610 --> 00:05:28,110 You just need to remember 110 00:05:28,110 --> 00:05:31,740 that this is how you pass in a mutable reference, 111 00:05:31,740 --> 00:05:33,750 and this is how you declare the parameter 112 00:05:33,750 --> 00:05:35,190 to be a mutable reference 113 00:05:35,190 --> 00:05:37,923 so that you can then change the underlying object.