The case for a garbage-collected type in Rust
I think Rust needs a GC type, like what @
was supposed to be.
I don't mean a reference-counted type, I mean an opt-in garbage-collected type, akin to Rc
or Arc
.
My case
It's quite simple.
Cyclic data structures
Sometimes you have cyclic data structures in Rust. Said reference cycles mean you need to manually implement Drop
. Sometimes you find yourself fighting the borrow checker over these cycles.
It's horrific pain and nobody should have to go through it.
We can avoid all this by having a Gc
or Agc
type that doesn't care how many cycles there are, it just does the right thing.
Garbage-collected languages
Implementing a garbage collector is basically mandatory for implementing any garbage-collected language, like JavaScript.
For Rust to be usable in these contexts, you either have to implement a GC yourself (which is horrific pain), or use an existing crate (none of them are ideal).
A built-in Gc
and Agc
type would help everyone in this.
Avoiding dangling pointers on Drop
When dropping to the level of pointers (which you basically have to do to implement anything with reference cycles), you run the risk of all sorts of problems associated with them.
I didn't choose Rust only to be forced into using it like C! It brings back the spectre of memory errors, use-after-free, off-by-one, etc.
We can avoid this whole mess with a Gc
type.
But isn't there the gc
crate?
Well, yes. But it can be quite a pain to use, and it's not as nice as something integrated into the language.
It also isn't thread-safe. Ideally, a GC type should be thread-safe.
But GC is evil!!!!!11111oneone!1!11
Yes, I know the arguments against GC. I am aware they tend to be non-deterministic, have the embarrassing pause, etc.
But I digress
For 99% of use cases, you absolutely do not need a GC. There's absolutely no reason to use one most of the time and in most applications.
But when you need one, you need one.
I genuinely hope this can become a thing someday in Rust.
— Elizabeth Ashford (Elizafox) Fedi (elsewhere): @Elizafox@social.treehouse.systems Tip jar: PayPal || CashApp || LiberaPay