3 ms·
To be fair, GC only collects memory and Rust's ownership manages ownership of other resources like files and ports and stuff too as I understand it!
by bglusman 7y ago
To be fair, GC only collects memory and Rust's ownership manages ownership of other resources like files and ports and stuff too as I understand it!
- twic 7y agoMore generally, it lets you tie resources to syntactic scopes. An example in the standard library is MutexGuard - you get one when you lock a mutex, and when you drop it, that releases the mutex. You can't forget to release the mutex, and you also can't release it too early, because then you lose access to the data protected by the mutex. There are ways to defeat this, of course, but you have to go out of your way to do it. I've used a library which uses a similar pattern for writing to a ring-buffered message queue: when you grab a slot in the queue to write to, you get a SlotGuard, and when you drop it, you lose access to the slot, but that commits the slot and lets other threads read it.