Unofficial Hacker News companion — get notified on comment replies, and more.

The state of Rust allocators in 2026: stabilised (opens external site)

219 points by fanf2 4 days ago | cetra3.github.io | 90 comments on HN | Vote on HN (opens Hacker News) |

View full thread · View on Hacker News

Comment thread (16)

  1. Good luck to all people who are going to try implementing allocators!

    Last time I tried, even the standard containers were giving different layouts to allocate and deallocate.

    Also as a bonus, you get to put another template parameter to all of your containers!

    Replies (4)
    1. Yet, in the lang adoption game, many who were looking at Zig thanks to native support for allocators will now re-look at Rust to solve their problem.
      Replies (1)
      1. And many who said they'd never use custom allocators when discussing using Zig will see how neat it is now that they can do it in Rust.
        Replies (2)
        1. Maybe. I don't expect this feature to have wide appeal. It's not useless, there are cases where you want this and it's great that it stabilized, but I think they're not very common and so in most cases as with everything else you shouldn't do this until you've got a problem it can solve.
        2. People have been able to do this in Rust for longer than Zig has existed. What this new development brings is a standardized interface to streamline interoperability, not any new language capability. The bumpalo crate has been the standard bearer here for nearly a decade now, currently counting 778 million downloads on crates.io.
          Replies (1)
          1. Without standardized interfaces, custom allocators have limiteduse. bumpalo could only be used seamlessly with its own custom types (like bumpalo::collections::Vec or bumpalo::boxed::Box.

            bumpalo::collections::Vec<'bump, T> is a completely distinct type from std::vec::Vec<T>. You cannot pass an arena-allocated Vec or String to third-party libraries that expect standard library types without copying or converting them to slices first

            Replies (1)
            1. > You cannot pass an arena-allocated Vec or String to third-party libraries that expect standard library types without copying or converting them to slices first

              Slices are already the standard for interoperability between Rust libraries, and always have been. There may very well be libraries out there whose APIs expect you to pass Vecs into them, but that would be highly atypical.

              Continue this thread (1 replies) →

    2. > Last time I tried, even the standard containers were giving different layouts to allocate and deallocate.

      Do you have some concrete examples? I couldn't reproduce this but maybe I'm looking in the wrong places.

      Replies (1)
      1. It was in this code.

        https://github.com/ozgrakkurt/s_alloc

        Test were failing until adding this

        https://github.com/ozgrakkurt/s_alloc/blob/42adbd31e9835cc17...

        Also I remember finding that allocator trait explicitly mentions this after finding the error.

        Replies (1)
        1. Ah I see, that makes sense.
    3. do those of us who come from a c++ background this all looks oddly familiar and equally painful
      Replies (1)
      1. AIUI the problem in C++ was that until C++ 20 it didn't have a slice type and so you couldn't communicate that you aren't allocating or de-allocating anything and so you don't care about allocators. Which means if you use either Allocator mechanism in C++ every part of the software needs to care about allocators. Rust doesn't have that problem

        Vec<u8,Goose> and Vec<u8,Global> both implement AsRef<[u8]> and AsMut<[u8]> which means code which doesn't want to grow or shrink the Vec needn't care about the Goose allocator or interact with it in any way.

        In C++ today you can write this way using std::span<T> from C++ 20, and enough people write C++ 20 that it probably won't cause overwhelming anger from people you've shut out, but in C++ 17 it could not be expressed at all, so that means even today some places cannot use your code.

    4. > Last time I tried

      For the benefit of the reader, this was two years ago.

      The OP references updates made in Rust 1.100, released three weeks ago.

      Replies (1)
      1. I think what they're seeing is the deallocation layout does "fit" but it has a different size. Rust's design for Allocator says that's fine, but if your code needs to know the size to find an allocation that's an issue for you because Rust doesn't promise it has the same size.

        Rust says allocation A will "fit" in allocation B if A's alignment is equal to B's and A's size is no larger than B's size.