- Wow. So that's a lot we've covered.
Let's have an example and then we'll run the example
to see how it all hands together.
So let's do that.
Here we are then in our source code in main.
We're going to look at this demo here,
demo passing references.
And it's here I've got a function.
That function one, function one takes a string reference
and an integer reference.
And it uses the star to dereference
to give me back the integer that I first thought of.
It's like a pointer, really.
Give me the contents
of the integer, the second exam mark.
And if the value is greater than 50, then it prints out.
It'll print out the contents
of the integer and it'll print out upon the string.
It'll grab the contents of the string reference,
the contents of the string,
convert the string to uppercase, and then display it like so
with a smiley face to indicate
that you passed the exam because you got more than 50%.
Or if the contents of the integer is less than 50
or then it'll say, sadly you failed with a sad face.
And again, it uses the star
to dereference the integer reference
and to dereference the string reference.
And as I said, when you're invoking a method on an object
if you have a reference, you can write it that way
or you can just write it this way.
You can just say reference.method
and that would also work as well.
So in practice, that is what you would do.
You wouldn't bother dereferencing it
if it's gonna happen automatically.
Rust has special dispensation.
When it sees a reference
and then a method being invoked upon it,
it'll automatically give you the object
to dereference and invoked like so.
So this first function takes a string reference.
I can only pass in an actual string object into here.
Okay, so up here in my main code when I call some func1.
I give it a reference to n, my integer, 42.
That's gonna fail unfortunately.
I've got a string object here.
I can pass a reference to the string.
That'll work.
But I can't pass in a string literal,
as I was saying, when you have a string reference
you can only pass in a string object reference.
You can't pass in a string literal.
That would give me a compiler error,
which would be a shame.
So I've got an improved version, some func2
The only difference is that
this one takes in a string slice, okay?
The rest of the function is the same as before,
except that it takes a string slice.
And that means they can take either a reference
to an actual string or just a string literal.
So when I call some func2 I can call it with a reference
to a string object, or I can pass in a string slice.
Okay, so a string reference decays can be converted
into a string slice.
So you can pass in a string reference
or just a slice, string text like so.
Okay and that would work.
And then finally,
my third function, some func3.
Here it is.
I pass in a reference to an integer
and a reference to a string, string slice, I should say.
So prompter zero is my integer reference.
I print that using the default formatter.
And I also print prompter one using the default formatter.
Okay, so this will print,
the default formatter will print the value
of the integer and the value of the string.
And then I print prompter zero and prompter one again
but this time using the pointer formatter.
So it'll print out string, sorry,
integer and string the addresses off.
Okay, so let's run the example.
Okay, so just from the top
I have an integer n that's a bad exam mark and Ola Nordmann.
I can pass those by reference.
And inside that function, it printed out the value
and the person's name and the fact that they failed, sadly.
In the second call I passed in a string reference
or a string slice.
Okay, so that was the example of the string reference
and that's the example of the string slice.
And then in the third example,
I pass in either a string object by reference
or a string slice.
Okay, so it prints out the values
using the default formatter
and then the address is using the pointer formatter
both for the first call to the function on line 14 above
and then on line 15 using Per Nordmann.
And then finally, just to remind you
that when you're passing by reference
you don't lose ownership of the underlying variables.
So I can still use s at the end of all of this.
I can still use s.
Okay, so it prints out s here.
There we are.
So a lot to cover
but it is very important that you know
passing by reference is the main way
that you avoid losing ownership
of objects when you pass them into functions.