Menu
Sign In Search Podcasts Libraries Charts People & Topics Add Podcast API Blog Pricing

Alice Ryhl

๐Ÿ‘ค Speaker
505 total appearances

Appearances Over Time

Podcast Appearances

The Pragmatic Engineer
Why Rust is different, with Alice Ryhl

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.

The Pragmatic Engineer
Why Rust is different, with Alice Ryhl

Now, if A was also valid, then when they both went out of scope,

The Pragmatic Engineer
Why Rust is different, with Alice Ryhl

it would clean up the string twice, which is not legal.

The Pragmatic Engineer
Why Rust is different, with Alice Ryhl

In most other languages, the garbage collector takes care of this.

The Pragmatic Engineer
Why Rust is different, with Alice Ryhl

A and B just go out of scope, do nothing, and then later it cleans up the string.

The Pragmatic Engineer
Why Rust is different, with Alice Ryhl

But here, we actually have to do it when we go out of scope.

The Pragmatic Engineer
Why Rust is different, with Alice Ryhl

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.

The Pragmatic Engineer
Why Rust is different, with Alice Ryhl

In Rust, we have a bunch of different types, which are essentially different kind of pointers.

The Pragmatic Engineer
Why Rust is different, with Alice Ryhl

And one of the pointers is it's called arc and it's reference counter.

The Pragmatic Engineer
Why Rust is different, with Alice Ryhl

And so the idea is you have some object somewhere and then you have an arc to the object.

The Pragmatic Engineer
Why Rust is different, with Alice Ryhl

And what you can do is you can call clone on the arc and this increments a counter.

The Pragmatic Engineer
Why Rust is different, with Alice Ryhl

And now you have two arcs to the same underlying memory.

The Pragmatic Engineer
Why Rust is different, with Alice Ryhl

So the object might be really big, but you have two arcs that share the same memory.

The Pragmatic Engineer
Why Rust is different, with Alice Ryhl

And when they go out of scope, the counter is just decremented.

The Pragmatic Engineer
Why Rust is different, with Alice Ryhl

And when the counter reaches zero, the object is cleaned up.

The Pragmatic Engineer
Why Rust is different, with Alice Ryhl

So this is a way of saying this object actually needs to be in multiple places.

The Pragmatic Engineer
Why Rust is different, with Alice Ryhl

There's no one place that owns it.

The Pragmatic Engineer
Why Rust is different, with Alice Ryhl

And so this way you can use a counter to know how many owners are there.

The Pragmatic Engineer
Why Rust is different, with Alice Ryhl

And then when the last owner goes away, it gets cleaned up.

The Pragmatic Engineer
Why Rust is different, with Alice Ryhl

So another pointer type we have in Rust is called the reference.