1 00:00:06,280 --> 00:00:13,960 In this section, we're going to take a look at references and borrowing references allow us to make 2 00:00:13,960 --> 00:00:19,390 a reference to a value without taking ownership of it. 3 00:00:19,870 --> 00:00:20,420 Okay. 4 00:00:20,530 --> 00:00:22,000 We're borrowing the value. 5 00:00:22,930 --> 00:00:26,590 We have two types of references shared and mutable. 6 00:00:27,010 --> 00:00:32,080 A shared reference is going to let you read but not modify whatever is being referenced to. 7 00:00:32,350 --> 00:00:37,450 You can have as many shared references to a particular value at a time as you desire. 8 00:00:37,840 --> 00:00:43,000 Mutable references, on the other hand, allow you to both read and modify the value. 9 00:00:43,990 --> 00:00:51,790 However, unlike a shared reference, you cannot have any other references of any sort to that value 10 00:00:51,790 --> 00:00:53,200 active at the same time. 11 00:00:53,530 --> 00:00:55,240 So let's take a look at this. 12 00:00:56,020 --> 00:01:05,260 So we'll start out by saying let's equal string from hello. 13 00:01:06,550 --> 00:01:20,650 And then we want to create a function called change string and we want to take in some string of a reference 14 00:01:20,650 --> 00:01:21,670 to a string. 15 00:01:23,110 --> 00:01:26,200 And then we want to take some string and. 16 00:01:27,160 --> 00:01:30,610 Push something onto it. 17 00:01:33,610 --> 00:01:43,510 So here we want to say change a reference and then to s so we make a reference. 18 00:01:46,720 --> 00:01:47,440 I. 19 00:01:48,340 --> 00:01:55,930 Using the ampersand sign so we can see we're already getting an error here and it's telling us, Hey, 20 00:01:55,930 --> 00:01:58,930 we need to make some string mutable. 21 00:01:58,930 --> 00:02:03,790 So the first thing we need to do is say Let s be mutable. 22 00:02:04,090 --> 00:02:06,430 And then here we need to make. 23 00:02:07,170 --> 00:02:09,690 It's known that it's going to be a mutable reference. 24 00:02:09,930 --> 00:02:13,650 And then down in here, we've got to do the same as well. 25 00:02:13,830 --> 00:02:18,510 So now all of our compiler errors are are gone. 26 00:02:19,050 --> 00:02:20,730 So let's see. 27 00:02:21,560 --> 00:02:23,530 What happens when we print it out. 28 00:02:23,540 --> 00:02:30,020 And so we're expecting SSE to come back and say Hello World, which is exactly what happens. 29 00:02:30,020 --> 00:02:36,350 And we were able to do this because we added the mute keyword, so making it mutable to the variable 30 00:02:36,350 --> 00:02:38,210 and inside the function argument. 31 00:02:39,210 --> 00:02:46,350 So after running cargo run, we were able to see that we change the variable without ever taking ownership 32 00:02:46,770 --> 00:02:49,920 of the variable inside the function. 33 00:02:49,920 --> 00:02:55,920 Because if we didn't pass it by reference, then change string would have taken ownership of S. 34 00:02:55,920 --> 00:02:59,820 Therefore, S would not have been able to have been used here. 35 00:03:00,060 --> 00:03:07,260 So that is the power of references and borrowing and rust and it's a concept that we are going to use 36 00:03:07,260 --> 00:03:09,840 throughout the remainder of this course. 37 00:03:09,840 --> 00:03:12,420 So it's very important to understand this. 38 00:03:13,970 --> 00:03:19,520 The next section, we're going to start taking a look at Starbucks and Lifetime's and continue building 39 00:03:19,520 --> 00:03:24,020 upon this idea of ownership, referencing and borrowing.