Alice Ryhl
๐ค SpeakerAppearances Over Time
Podcast Appearances
And of course, this is important when we don't have a garbage collector because then when B goes out of scope, it has to clean up the string.
Now, if A was also valid, then when they both went out of scope,
it would clean up the string twice, which is not legal.
In most other languages, the garbage collector takes care of this.
A and B just go out of scope, do nothing, and then later it cleans up the string.
But here, we actually have to do it when we go out of scope.
And the ownership model allows you to move from A to B, and then A becomes inactive or unusable and does not get cleaned up because it doesn't have a value to clean up anymore.
In Rust, we have a bunch of different types, which are essentially different kind of pointers.
And one of the pointers is it's called arc and it's reference counter.
And so the idea is you have some object somewhere and then you have an arc to the object.
And what you can do is you can call clone on the arc and this increments a counter.
And now you have two arcs to the same underlying memory.
So the object might be really big, but you have two arcs that share the same memory.
And when they go out of scope, the counter is just decremented.
And when the counter reaches zero, the object is cleaned up.
So this is a way of saying this object actually needs to be in multiple places.
There's no one place that owns it.
And so this way you can use a counter to know how many owners are there.
And then when the last owner goes away, it gets cleaned up.
So another pointer type we have in Rust is called the reference.