All right.
In this lecture, let's take a little bit of a more thorough look at how moves work.
So the first thing we're going to do, we're going to create a string and say string from an we'll say
takes because we're going to take ownership away from the string through a function.
So we're going to call this F and takes ownership and we are going to take a string.
And then.
And here we will just say.
Let.
During equal se.
Okay.
So what's going to happen in this is I guess I should call it.
So takes ownership and we pass in SX and then in here we'll print it out and we'll print out string.
So what happens here?
So we create a variable.
With a string takes.
And then in here, we give ownership to the function.
Right.
So we're taking this variable, giving it ownership inside of this function and we're using it in here.
So if we cargo run it, we can see that we're in here inside this function.
So.
Just to make sure that the ownership was taken properly.
Let's actually and to be lazy, I'm going to copy and paste and it's not even going to let us build
and run.
But again, earlier on in this lecture, we saw the same errors where it talks about how we move the
value and then we try to reference the value borrowed after the move so we can see that we pass ownership
to functions when we pass the values through parameters.
So what about.
Passing a value as a copy.
So we'll say let value equals one.
And then we'll we'll do the same thing.
We'll just say, hey, how about make a copy of it?
So then we'll call this one because that's what we're going to pass in and we'll just make it of I 32.
And down here we'll say let vow one equals one.
And we'll print out vowel one.
And then here we will say make copy of vow.
And we make a copy here because.
This is an AI 32 which implements the copy trade.
So again, if we run it, we'll see one printed out.
And same concept applies.
What we learned.
Last lecture of.
When it's a certain variable, some of them implement the copy trade.
And I 32 implements copy.
Therefore the function does not take.
Ownership of it.
So we can also give ownership.
So we'll say give ownership and and here we'll do another string, string, one string and we want to
pass it back a string.
And then we will say.
Given not to string.
Got an error.
What do we got?
So up here we will have let type string equals.
Give ownership.
Oh, and we don't want to pass a parameter.
And I see what my issue is down below.
Get rid of that.
So we called give ownership, which owns given it owns this, even though it's not technically a parameter,
it still owns the value and we can print it out and show that we now have ownership of it.
So.
And it was given.
So down here we passed ownership back up through a.
Returning.
So now we can also do both of them so we can.
Take and give.
So let's do another string.
Call it string to string and it's going to return a string.
And we'll just do that or return exactly what is given back.
So we'll say let string three type string equals.
Take and give but of string one.
So now I expect string one to print out, given which it does, and to prove that we took it.
Let's try printing out string one and see what happens.
So we already got our air, which is a good sign so we can see that we are able to take and give ownership
through functions.
Well, what about with control flow statements?
Do they work the same?
So let's take a let's take a look at that just to see what is going to happen with those.
So we'll again, keep super simple concepts here and we'll say let string four equals string three else.
And this obviously never get run because we have true in the block above but just for sake of it string
five also equals.
Stream three.
So what happens if we try to print out string three?
It's given us that air.
Sounds like a good sign.
Let's see what it says.
Move KERS.
Because string three is type.
String does not implement copy that checks out at line 35.
The value is moved here.
So also in control flow statements.
Ownership operates the same as if they're not there.
So.
You can pass ownership through those as well.
And lastly, let's look at a loop.
So for this, let's just do a kind of a clean slate.
Let's comment all this out.
Comment all this out to suppress any airs or warnings?
I should say.
So let's create a mutable string to.
And then let's just call.
Loop.
And I actually want to.
He will give let mut str one equal.
Let's drink from.
Tyler.
So a loop.
Remember, this is just going to be an infinite loop that happens non-stop.
So let's assign STR two to STR one.
And what do we get?
So we also have an ownership issue here.
And this is actually really important to note because remember, this is an infinite loop.
But imagine if you have a for loop or a while loop based on some kind of condition.
Well, if you.
Change ownership of a vowel of a variable or a value, I should say, inside a loop.
That ownership changes so that ownership needs to be able to stay alive on that.
Value for the entirety of the loop, because if you change ownership, you might break the loop, which
is what happens in this case, because it says here the value was moved here and previous iteration
of a loop.
So those are just a couple of more examples of how ownership works.
And I did not want to spend as much time typing out the codes as going over the concept.
So that was why it was a lot of copying and pasting.
But all of these concepts, we will practice a lot throughout this course.
So I really just wanted to make sure I introduced them just to really emphasize how important these
concepts are for understanding the rust language.