1 00:00:05,680 --> 00:00:10,150 Ref cell allows us to follow the interior mutability pattern. 2 00:00:10,420 --> 00:00:17,200 Interior mutability is a design pattern that allows us to mutate data even when there are immutable 3 00:00:17,200 --> 00:00:18,850 references to that data. 4 00:00:18,970 --> 00:00:22,270 This is typically not allowed, as we learned earlier in the course. 5 00:00:22,270 --> 00:00:28,330 So this does use unsafe code to Ben Russ's usual rules of mutation and borrowing. 6 00:00:29,700 --> 00:00:36,090 Ref sells rules are enforced at runtime, meaning the compiler typically won't catch any errors for 7 00:00:36,090 --> 00:00:36,510 us. 8 00:00:36,510 --> 00:00:42,270 So if an error is presented at runtime, then our program will panic and terminate. 9 00:00:42,660 --> 00:00:45,690 So let's begin to look at an example using ref. 10 00:00:45,690 --> 00:00:53,310 So the first thing we need to do is import it in and it is in standard cell ref cell. 11 00:00:54,270 --> 00:00:58,410 And now we will create a new struc called Flieger. 12 00:00:58,410 --> 00:01:03,840 And this is just going to be a simple stroke in order to demonstrate the idea behind a ref cell. 13 00:01:05,100 --> 00:01:08,430 So we're going to have one field called is true. 14 00:01:08,460 --> 00:01:13,080 That's going to be a type ref cell, which is looking for a boolean. 15 00:01:16,010 --> 00:01:20,810 So now we will say let flag equals flag. 16 00:01:23,960 --> 00:01:31,460 Is true ref sell new and then we want our boolean to be true in this case. 17 00:01:32,730 --> 00:01:37,560 So when we use ref's cell, we gain access to two methods. 18 00:01:37,560 --> 00:01:40,380 Borrow and borrow mute. 19 00:01:42,030 --> 00:01:58,320 Borrow returns the smart pointer as a reference to t so bar row returns ref t and then bar row mute 20 00:01:58,320 --> 00:02:09,120 returns ref mute to type T and both of these implement DX referencing so we can treat them like regular 21 00:02:09,120 --> 00:02:10,140 references. 22 00:02:11,850 --> 00:02:16,470 So we'll say let reference equals flag ish. 23 00:02:16,470 --> 00:02:17,190 True. 24 00:02:20,010 --> 00:02:20,970 But borrow. 25 00:02:23,160 --> 00:02:31,140 And now we will print this out to make sure that the value of true is what was borrowed. 26 00:02:34,330 --> 00:02:35,140 And we have. 27 00:02:35,140 --> 00:02:35,740 True. 28 00:02:35,980 --> 00:02:38,080 So now we can comment this out. 29 00:02:40,010 --> 00:02:45,160 And let's do a mutable borrow. 30 00:02:45,170 --> 00:02:50,960 So we'll say let mute mute ref equals flag. 31 00:02:50,960 --> 00:02:51,800 Dot ish. 32 00:02:51,800 --> 00:02:52,990 True dot. 33 00:02:53,540 --> 00:02:54,050 Borrow. 34 00:02:54,050 --> 00:02:54,530 Mute. 35 00:02:55,810 --> 00:02:58,030 And then we will reference it. 36 00:02:58,030 --> 00:03:03,070 That way we can access the value inside of here, which in this case is currently true. 37 00:03:03,070 --> 00:03:10,720 So we want to reference it to have direct access to it and now we want to change it to false DX reference 38 00:03:10,720 --> 00:03:17,350 first to access inside and now when we print this out. 39 00:03:21,780 --> 00:03:24,450 We expect it to print out false. 40 00:03:25,350 --> 00:03:26,370 This is what we got. 41 00:03:26,370 --> 00:03:31,020 So we were able to mutable change and end mutable value. 42 00:03:32,260 --> 00:03:37,990 And now what happens if we comment them both in and then rerun it? 43 00:03:38,560 --> 00:03:44,200 So when we commented in and rerun it, we see that it ran fine, fine, fine. 44 00:03:44,200 --> 00:03:45,400 And then it panicked. 45 00:03:46,090 --> 00:03:51,640 So when we have them both commented in, we get an error stating that it was already borrowed. 46 00:03:51,640 --> 00:03:56,920 And this is because it wasn't checked at the compile time, but it was checked at runtime. 47 00:03:57,130 --> 00:04:03,070 So we cannot have multiple borrows out unless we pair it with an RC. 48 00:04:03,070 --> 00:04:04,810 So let's see what that looks like. 49 00:04:06,340 --> 00:04:08,020 So we will change. 50 00:04:09,440 --> 00:04:10,010 Here. 51 00:04:10,600 --> 00:04:11,290 To. 52 00:04:12,890 --> 00:04:18,920 B RC and then reference have a ref cell inside of it. 53 00:04:20,360 --> 00:04:21,290 And now. 54 00:04:23,140 --> 00:04:28,300 We will say our sea new. 55 00:04:35,440 --> 00:04:41,530 Now we can say our new reference here will just delete all of this is RC. 56 00:04:41,530 --> 00:04:46,660 New flag dot is true dot clone. 57 00:04:49,710 --> 00:04:56,940 And now when we run this, we will need to make that in debug mode. 58 00:04:57,810 --> 00:05:05,130 And now when we run it, we see that we have we printed out a ref cell which points to the value of 59 00:05:05,130 --> 00:05:05,790 true. 60 00:05:06,450 --> 00:05:10,470 So now we have created another owner pointing to our. 61 00:05:11,230 --> 00:05:13,840 Rough sell which is holding the value of true. 62 00:05:13,840 --> 00:05:20,200 And if we wanted to change the value through our shared owner, we can do it this way so we can say 63 00:05:20,200 --> 00:05:21,760 Let mute ref. 64 00:05:22,690 --> 00:05:23,590 Equals. 65 00:05:24,920 --> 00:05:26,120 Reference. 66 00:05:26,570 --> 00:05:29,660 Borrow mute and now we can change it to false. 67 00:05:29,660 --> 00:05:31,130 And now we can print it out again. 68 00:05:31,130 --> 00:05:33,200 And it will print out. 69 00:05:33,940 --> 00:05:35,140 And run just fine. 70 00:05:35,140 --> 00:05:38,560 And we have the same results as we did earlier. 71 00:05:39,910 --> 00:05:46,960 So let's summarize really quickly on when to choose box RC or ref SEL. 72 00:05:47,440 --> 00:05:56,890 So RC enables multiple owners of the same data, whereas box and ref sale only have single owners. 73 00:05:57,280 --> 00:06:04,060 Box allows immutable or mutable borrows checked at compile time. 74 00:06:04,360 --> 00:06:14,740 RC allows only immutable, borrows checked at compile time and ref sel allows immutable or mutable, 75 00:06:14,740 --> 00:06:17,320 borrows checked at runtime. 76 00:06:17,530 --> 00:06:25,120 And then because ref sells allows mutable, borrows, checked at runtime, you can mutate the value 77 00:06:25,120 --> 00:06:30,520 inside the ref cell even when the ref cell is immutable. 78 00:06:31,000 --> 00:06:38,440 So again, smart pointers are very powerful and allowing us to manipulate data and store it on the heap 79 00:06:38,440 --> 00:06:44,200 and allow us to do a lot of very unique things with the values inside. 80 00:06:44,800 --> 00:06:52,210 And so we will be able to take these concepts and apply them in the next section when we start to talk 81 00:06:52,210 --> 00:06:53,710 about concurrency.