In this section, we're going to take a look at references and borrowing references allow us to make
a reference to a value without taking ownership of it.
Okay.
We're borrowing the value.
We have two types of references shared and mutable.
A shared reference is going to let you read but not modify whatever is being referenced to.
You can have as many shared references to a particular value at a time as you desire.
Mutable references, on the other hand, allow you to both read and modify the value.
However, unlike a shared reference, you cannot have any other references of any sort to that value
active at the same time.
So let's take a look at this.
So we'll start out by saying let's equal string from hello.
And then we want to create a function called change string and we want to take in some string of a reference
to a string.
And then we want to take some string and.
Push something onto it.
So here we want to say change a reference and then to s so we make a reference.
I.
Using the ampersand sign so we can see we're already getting an error here and it's telling us, Hey,
we need to make some string mutable.
So the first thing we need to do is say Let s be mutable.
And then here we need to make.
It's known that it's going to be a mutable reference.
And then down in here, we've got to do the same as well.
So now all of our compiler errors are are gone.
So let's see.
What happens when we print it out.
And so we're expecting SSE to come back and say Hello World, which is exactly what happens.
And we were able to do this because we added the mute keyword, so making it mutable to the variable
and inside the function argument.
So after running cargo run, we were able to see that we change the variable without ever taking ownership
of the variable inside the function.
Because if we didn't pass it by reference, then change string would have taken ownership of S.
Therefore, S would not have been able to have been used here.
So that is the power of references and borrowing and rust and it's a concept that we are going to use
throughout the remainder of this course.
So it's very important to understand this.
The next section, we're going to start taking a look at Starbucks and Lifetime's and continue building
upon this idea of ownership, referencing and borrowing.