- In this section, we're going to look at how to pass
multiple references into a function.
First of all, a quick recap of how to pass
immutable references into a function.
Have a look at this example here.
I have a function that takes an immutable reference to &i32
and I pass in a reference there, I borrow it,
and it takes an immutable reference to a string
which I pass in here.
I borrow the string in here.
So inside that function,
we can access the original contents,
but we can't modify the values.
Okay, so if you do want to modify a value,
then you can pass in a mutable reference.
Obviously, the object itself
must originally be mutable to support this.
So when you pass in a parameter,
precede the parameter name with &mut
as a multiple reference.
Here is an example.
I'm calling in a function
and passing in a mutable reference to my integer n
and I'm passing in a mutable reference to my string s.
So the function will receive a reference,
a mutable reference,
that it can change to the integer and to the string.
Right, well, inside the function,
when you receive a mutable reference, remember in Rust,
a reference is really like a pointer.
So you have to use the star to dereference
and get back to the original value,
which you can then change.
So here's a function.
It receives a mutable reference to an integer.
Okay, so basically it's like a pointer really
that points to some integer up here
and it receives and I can then change it here.
I can use the star to basically access the contents
of where the reference is pointing to and change that value.
And in exactly the same kind of way
for my string reference parameter,
sparam is a mutable reference to a string.
So it kinda points to some string object over here
that contains some text, something like that.
And then in order to actually change that text,
we can dereference to get back to the original string
and then we can change the value in the string.
We can append World, for example, like that.
Okay, so just one other thing to remember.
When you're invoking a method on a reference,
you can dereference explicitly
to get back to the original object,
but Rust also supports the shorthand syntax.
It'll automatically,
when you invoke a method on a reference,
it'll automatically dereference as if you wrote it that way.
So in practice, people always write it this way
because it's easier.
Right, well, let's see an example.
In Lesson 8 functions, we'll have a look at the demo
passing mutable references example and then we'll run it.
Okay, so then in the main code, let me uncomment
demo_passing_mutable_references,
and here it is.
So I have a function that receives a mutable reference
to an integer and a mutable reference to a string.
It outputs the values initially.
Remember, when you have references,
the default formatter automatically dereferences for you.
So when you use the default formatter here,
it's as if you put a star here
except that you don't actually have to.
It automatically dereferences for you
when you use the default formatter, which is quite nice.
And then we use the star
to increment the value of the number.
And I could have also used the star here for my method call,
but I've chosen not to because it would work anyway.
So whatever string I'm referring to
will now have World appended
and then it outputs the values after that.
Let's run it and see what happens.
Okay, so from the top, n is initially 42
and the string is hello,
pass in a mutable reference to n
and a mutable reference to s.
Okay, so the values can be changed.
It outputs the initial values of n and s
which are 42 and hello.
It changes the values, it adds 10, and it adds a string,
and then it prints the values after changing the number
and after changing the string.
It outputs the value afterward, 52, Hello World.
When you refer to a string,
it is the original string that you're updating.
Okay, so it is the actual real string that's been changed
and the real integer that's been changed.
So when that function returns,
it's the real values that have changed,
n is 52 and the string is hello world.
I change it again.
We still own these values
so I'm gonna add a million and some thumbs up
and then I print the value of n and s afterwards.
So afterwards, n is 1,000,052 and s is Hello World
thumbs up, thumbs up, thumbs up.
Okay, so that's actually quite straightforward.
You just need to remember
that this is how you pass in a mutable reference,
and this is how you declare the parameter
to be a mutable reference
so that you can then change the underlying object.