What does Rust have instead of a garbage collector?

前端 未结 3 569
半阙折子戏
半阙折子戏 2021-01-30 00:20

I understand Rust doesn\'t have a garbage collector and am wondering how memory is freed up when a binding goes out of scope.

So in this example, I understand that Rust

3条回答
  •  情深已故
    2021-01-30 01:20

    Garbage collection is typically used periodically or on demand, like if the heap is close to full or above some threshold. It then looks for unused variables and frees their memory, depending on the algorithm.

    Rust would know when the variable gets out of scope or its lifetime ends at compile time and thus insert the corresponding LLVM/assembly instructions to free the memory.

    Rust also allows some kind of garbage collection, like atomic reference counting though.

提交回复
热议问题