3 ms·
Congratulations to the Rust team! Can't wait to start learning the language and building stuff using it. I'm looking to learn about how Rust's refcounting memo
by austinz 12y ago
Congratulations to the Rust team! Can't wait to start learning the language and building stuff using it.
I'm looking to learn about how Rust's refcounting memory management works (and how it differs from how, e.g. Objective-C or Swift's runtime-based reference counting works), mostly for personal edification. Can anyone point me to any good resources?
- steveklabnik 12y agoIs worth noting that you don't reach for reference counting by default in Rust: your reach for references, then boxes, THEN Arc. You can find documentation on these types here: http://doc.rust-lang.org/guide-pointers.html http://doc.rust-lang.org/guide-pointers.html
- austinz 12y agoAwesome! Thanks.
- eddyb 12y agoWell, you wouldn't go for Arc (atomic RC) unless you need to share data across tasks (threads), within the same task you can use Rc. This is different from shared_ptr in C++ which is always atomic and LLVM has to try hard (in clang, no idea what GCC does) to eliminate some redundant ref-counts. Oh and since Rc is affine, you only ref-count when you call .clone() on it or you let it fall out of scope. Most of the time you can pass a reference to the contents, or, if you need to sometimes clone it, a reference to the Rc handle, avoiding ref-counting costs.
- steveklabnik 12y agoThat's true, thanks.
- burntsushi 12y agoSteve already told you that refcounting isn't reached for by default, and I wanted to share an anecdote to emphasize that. I've written a regex library, CSV parser, command line arg parser, elastic tabs and quickcheck in Rust. Behold: $ find ./ -type f -name '*.rs' -print0 | xargs -0 grep Rc $ I've definitely reached for it a few times as an escape hatch, but I've always ended up finding a cleaner approach to persist without it. Of course, there are plenty of legitimate uses of refcounting. I just haven't hit them yet. :-)
- steveklabnik 12y agoYou're logged in as root? O_O :p :)
- burntsushi 12y agoHaha, no, I added that in after-the-copy-and-paste. :P Fixed nonetheless. Do'h.