5 ms·
> Rust people keep saying they have no Garbage Collector, when they have a Region Based Garbage Collector. Rust does not have a region based garbage collector.
by Diggsey 5y ago
> Rust people keep saying they have no Garbage Collector, when they have a Region Based Garbage Collector.
Rust does not have a region based garbage collector. Memory management in Rust works exactly the same as in does in C++ (ie. destructors are automatically called when variables go out of scope, and those destructors may deallocate memory).
The borrow checker is purely a check: not only does it operate entirely at compile-time, it does not even influence code generation. The only thing the borrow checker can do is prevent code from compiling.
- zamalek 5y agoCorrectly/academically speaking, a GC emulates infinite memory[1]. It's a misnomer (because the fact/name predated the theory). In the event that you actually have infinite memory, NOOP could be classified as a GC. Academically speaking, Rust is a managed language. The GC has simply been lifted to compile-time (or runtime in the case of Rc). You never call `alloc` and you never call `free`. [1]: https://devblogs.microsoft.com/oldnewthing/20100809-00/?p=13203 https://devblogs.microsoft.com/oldnewthing/20100809-00/?p=13...
- miguelraz 5y agoFair enough, thanks for the pointer, I've amended the most recent version of the post and given you credit.
- Ar-Curunir 5y ago> You never call `alloc` and you never call `free`. That’s not a property of the language, only of the standard library. You could design APIs that do require you to manually call alloc and free; it’s just less ergonomic to do that
- Diggsey 5y agoBy that definition C++ is also garbage collected and managed. Given that C++ is oft cited as the canonical unmanaged non-GCed language, this seems like a not very useful definition. (And you can call `alloc` and `free` from Rust, https://doc.rust-lang.org/std/alloc/fn.alloc.html https://doc.rust-lang.org/std/alloc/fn.alloc.html) It's also weird that you call out `Rc` specifically. `Box` also allocates and free memory, as does `Vec` and other types from the `alloc` crate.
- pjmlp 5y agoActually, like everything in computing, it depends. Reference counting is a form of garbage collection, as per most highly referenced CS books on the subject, like The Garbage Collection Handbook, chapter 5. https://gchandbook.org/ https://gchandbook.org/ But lets put that aside, considering that std::..._ptr<>() or any other variant thereof are plain optional library types. That still leaves Unreal C++, C++/CLI, C++/CX as garbage collection extensions to C++, just like GCC C++ or clang C++ provide their own language extensions. https://docs.unrealengine.com/4.26/en-US/ProgrammingAndScripting/ProgrammingWithCPP/UnrealArchitecture/Objects/Optimizations/ https://docs.unrealengine.com/4.26/en-US/ProgrammingAndScrip... https://docs.microsoft.com/en-us/cpp/dotnet/dotnet-programming-with-cpp-cli-visual-cpp?view=msvc-160 https://docs.microsoft.com/en-us/cpp/dotnet/dotnet-programmi... https://docs.microsoft.com/en-us/cpp/cppcx/visual-c-language-reference-c-cx?view=msvc-160 https://docs.microsoft.com/en-us/cpp/cppcx/visual-c-language...
- miguelraz 5y agoRight, and those destructors get called at the of the variable's scope. Is there a reason why it's not well-founded to consider scopes to be regions?
- steveklabnik 5y ago> Is there a reason why it's not well-founded to consider scopes to be regions? You're running into a colloquial vs formal distinction, I think. Yes, a scope is a "region" in the sort of general sense, but in PLT, "region based memory management" is jargon: https://www.cs.umd.edu/projects/cyclone/papers/cyclone-regions.pdf https://www.cs.umd.edu/projects/cyclone/papers/cyclone-regio... If you check 2.1 of that paper, you'll see it's something sort of like "arenas with optional GC if you want to reclaim memory", which is quite different than what Rust does by default.
- miguelraz 5y agoThanks, for the paper Steve, I'll edit the post with due credit. What would be the appropriate description for Rust's memory model? Automatic scoped memory management with opt-in reference counting?
- Diggsey 5y ago"Memory model" is also an academic term which (among other things) describes how memory behaves WRT to concurrent opreations, so I would not use it in this context. Memory management is the correct term. Rust the language does not do any memory management: memory management is a feature of the standard library. I would say that the Rust standard library (the `alloc` crate specifically) provides types that safely encapsulate allocation and deallocation of memory. This is enabled by automatic destructor calls on stack-allocated variables when they go out of scope. (However, note that destructors need not deallocate memory). Again, this part works exactly the same in C++. In C++ when a type's constructor acquires some resource, and its destructor releases that resource (in this case the resource being memory) it is referred to as "Resource Acquisition Is Initialization" or RAII. https://en.wikipedia.org/wiki/Memory_model_(programming) https://en.wikipedia.org/wiki/Memory_model_(programming)
- cwp 5y agoRust has a static garbage collector.
- kzrdude 5y agoC has one too, it cleans up automatic variables.