modulerust

What is the difference between `alloc::rc::Rc` and `std::rc::Rc`?


I'm curious is there a difference between these two modules in practice? And if not, why have these two duplicates then?


Solution

  • std::rc::Rc is just a re-export of alloc::rc::Rc. You can see in src/std/lib.rs that the whole rc module is re-exported: pub use alloc::rc;

    The alloc crate is for any kind of memory allocations. Reference counted, Boxed, raw allocations and general access to the underlying allocator (often jemalloc in Rust). Since the Rc type is such a common type that it should exist in the standard library, but the alloc crate should not be a part of the standard library, just the rc module of alloc is re-exported to the standard library. This saves the user from having to care about the alloc crate, and instead offers a clean standard library without odd stuff that is prone to be uncomfortable to use.