Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How are Rust's Arc and Rc types different from having garbage collection?

The Rust Programming Language, first edition says that Rust does not have a garbage collector:

It maintains these goals without having a garbage collector

However, in discussing choosing your guarantees it also says:

Rc<T> is a reference counted pointer. In other words, this lets us have multiple "owning" pointers to the same data, and the data will be dropped (destructors will be run) when all pointers are out of scope.

As far as I understand, this is exactly how pointers work in a garbage-collected language like Python.

I consider garbage collection to be any process which prevents the need for manual deallocation of dynamically-allocated memory. I think I don't understand what the Rust guide considers to be garbage collection however.

like image 969
Mike Vella Avatar asked Dec 26 '14 21:12

Mike Vella


2 Answers

I consider garbage collection to be any process which prevents the need for manual deallocation of dynamically-allocated memory

Then Rust does have "garbage collection"!

fn make_stuff() {
    // Allocate memory on the heap and store `true` in it
    let thing = Box::new(true);
    // Allocate memory on the heap and store 1000 numbers in it.
    let things = vec![42; 1000];
} // Look Ma! No manual deallocation!

When thing and things go out of scope (in this case, at the end of the method), then the memory they had allocated will be freed for you.

Rc and Arc allow more flexibility than this; you should give their docs a read to know more.


In addition to @Manishearth's answer, there's also this detail (emphasis mine):

automatically freed at the end of its last owner's lifetime

In many garbage-collected languages, garbage collection happens out-of-band with the rest of your code. In Rust, the location of deallocation will be known.

Given this Java:

public static ArrayList alpha()
{
    return new ArrayList();
}

public static void beta()
{
    alpha(); // Unused result
}

I do not believe that you can say with certainty when the ArrayList will be removed from memory. In the equivalent Rust code, you know that an Arc or Rc will be destructed as soon as it goes out of scope.

like image 160
Shepmaster Avatar answered Sep 23 '22 21:09

Shepmaster


This article is a bit old, regarding how Rust has changed now, but it highlights what it means that Rust does not have GC. Only RAII and ownership are intrinsic to Rust. They helps writing reference-counting alike GCs such as Rc and Arc, but those are not part of the language, they're part of the standard library. And it makes a huge difference.

If you consider writing an operating system in Rust, you can't use any form of GC in part of your code or use the standard library. At this level, it's important to know what is part of the language and what is not. For a simple example, have look here.

In contrast, in a language such as Java or Python, you can't prevent your code from using the GC as it use it implicitly by design of the language.

In Rust, like in C/C++ a GC is part from a library and it's use is explicit.

like image 38
Nemikolh Avatar answered Sep 21 '22 21:09

Nemikolh