- In the previous section we talked about the Copy trait
and I also explained that most types in Rust
don't actually implement the Copy trait.
For example, String doesn't implement Copy.
Therefore for types that don't implement copy,
when you assign one value to another,
the value is moved from the first variable
into the second variable
and the original variable becomes invalidated
and you can't use it
and the compiler would complain if you try to.
So here's an example, just to recap what we already know.
In this example,
S1 is a String, let me draw it here.
S1 is a String.
It has a pointer effectively,
to a block of memory on the heap, hello.
And then we assign S1 into S2.
And because S1, sorry because String doesn't implement Copy,
then this will move the text from S1 into S2.
Well, actually what really happens
when it doesn't move,
it just basically does a bit copy of this into here.
So basically the pointer that was in there
gets copied into here.
So S2 points there.
But not only that, the compiler also then invalidates S1
as if S1 never existed, you can't use it anymore.
Its value has been moved.
So if you print S2, it'll print hello and that's fine.
But if you try to print S1, you'll get a compiler error
because S1 has lost its value.
Right, well what if you did want to actually copy the text
from S1 into S2 instead of just copying the pointer?
In situations like that, you can rely on the Clone Trait.
This is another important trait in Rust,
predefined in the Rust library.
The Clone Trait has a clone function which does a deep copy.
It returns a clone or a copy of the actual value,
like the text, not just the pointer.
So String implements Clone.
So you can call the clone method on a String.
You do actually have to call the clone method
to perform the copy and then you'll get back
a duplicate of the original text.
Right, you can also use the original text as well, okay?
So you'll end up with two Strings,
the original plus the copy, both of which are still alive.
The original String doesn't lose its value.
Here's an example of Cloning a String.
I've got S1.
Notice that S1 is mutable for the purposes of my example.
So let's draw that here.
S1 is a String object.
It has a point there to hello, like so.
And then when I call S1.clone,
the Clone function will basically
give me back another String,
and it'll kind of copy the text into here.
Okay, so a bit like a Copy Constructor in C++
There we go and that's S2.
So in my example,
for S1, I append world, okay, very exciting.
So hello world. S1 is now hello world.
But S2 is still just hello.
Yes, that's correct.
Right, so that was quite straightforward.
Most of the times, hopefully you don't call clone
because it's obviously, you know,
quite time consuming cloning the text.
But if you need to clone it, then you can.
Right, quick example to show how that all works in practice.
Let's go to the demo project lesson 6, Scope Ownership.
Have a look at demo cloning and then run it as usual.
Okay, so first of all, in my main code
let's un-comment the call to the demo cloning example,
demo_cloning::do_it.
Obviously I've imported, I've declared that module there.
And let's dive into it here.
Right, well, I'll run it and then we can discuss the output.
It's basically the same as we just saw
in the PowerPoint deck.
So cargo, run.
So first of all,
I've got some integer implements copy.
So there's no need to clone the value of a
when you have a type which implements copy.
When you assign, it is effectively doing a bit-wise copy.
So a and b are both 42, which is correct.
And then I've got a mutable String here at S1.
I clone its content.
So that S2 is also hello.
And then in S1 I append. Oh yes, I remember this example.
When I first taught Rust, it was in Norway
in a very nice conference center
where they served ice cream and popcorn and coffee.
So I append S1 was originally hello, hello world.
det er istid eller kaffetid snart
If you speak (speaks in foreign language)
if you speak Norwegian,
it is ice cream time or coffee time soon.
Okay, there we go.
So S1 has been appended, but of course S2,
S2 is just the original text.
S2 just says hello.
Right. So that concludes this lesson on Ownership.
What we'll have a look at in the next lesson
is a related concept of borrowing,
where you can effectively take a reference to an object
without claiming ownership, okay?
So it kind of fits in well
with what we've just covered in this lesson.