Ref cell allows us to follow the interior mutability pattern.
Interior mutability is a design pattern that allows us to mutate data even when there are immutable
references to that data.
This is typically not allowed, as we learned earlier in the course.
So this does use unsafe code to Ben Russ's usual rules of mutation and borrowing.
Ref sells rules are enforced at runtime, meaning the compiler typically won't catch any errors for
us.
So if an error is presented at runtime, then our program will panic and terminate.
So let's begin to look at an example using ref.
So the first thing we need to do is import it in and it is in standard cell ref cell.
And now we will create a new struc called Flieger.
And this is just going to be a simple stroke in order to demonstrate the idea behind a ref cell.
So we're going to have one field called is true.
That's going to be a type ref cell, which is looking for a boolean.
So now we will say let flag equals flag.
Is true ref sell new and then we want our boolean to be true in this case.
So when we use ref's cell, we gain access to two methods.
Borrow and borrow mute.
Borrow returns the smart pointer as a reference to t so bar row returns ref t and then bar row mute
returns ref mute to type T and both of these implement DX referencing so we can treat them like regular
references.
So we'll say let reference equals flag ish.
True.
But borrow.
And now we will print this out to make sure that the value of true is what was borrowed.
And we have.
True.
So now we can comment this out.
And let's do a mutable borrow.
So we'll say let mute mute ref equals flag.
Dot ish.
True dot.
Borrow.
Mute.
And then we will reference it.
That way we can access the value inside of here, which in this case is currently true.
So we want to reference it to have direct access to it and now we want to change it to false DX reference
first to access inside and now when we print this out.
We expect it to print out false.
This is what we got.
So we were able to mutable change and end mutable value.
And now what happens if we comment them both in and then rerun it?
So when we commented in and rerun it, we see that it ran fine, fine, fine.
And then it panicked.
So when we have them both commented in, we get an error stating that it was already borrowed.
And this is because it wasn't checked at the compile time, but it was checked at runtime.
So we cannot have multiple borrows out unless we pair it with an RC.
So let's see what that looks like.
So we will change.
Here.
To.
B RC and then reference have a ref cell inside of it.
And now.
We will say our sea new.
Now we can say our new reference here will just delete all of this is RC.
New flag dot is true dot clone.
And now when we run this, we will need to make that in debug mode.
And now when we run it, we see that we have we printed out a ref cell which points to the value of
true.
So now we have created another owner pointing to our.
Rough sell which is holding the value of true.
And if we wanted to change the value through our shared owner, we can do it this way so we can say
Let mute ref.
Equals.
Reference.
Borrow mute and now we can change it to false.
And now we can print it out again.
And it will print out.
And run just fine.
And we have the same results as we did earlier.
So let's summarize really quickly on when to choose box RC or ref SEL.
So RC enables multiple owners of the same data, whereas box and ref sale only have single owners.
Box allows immutable or mutable borrows checked at compile time.
RC allows only immutable, borrows checked at compile time and ref sel allows immutable or mutable,
borrows checked at runtime.
And then because ref sells allows mutable, borrows, checked at runtime, you can mutate the value
inside the ref cell even when the ref cell is immutable.
So again, smart pointers are very powerful and allowing us to manipulate data and store it on the heap
and allow us to do a lot of very unique things with the values inside.
And so we will be able to take these concepts and apply them in the next section when we start to talk
about concurrency.