You can absolutely use local allocators in Rust, but the APIs around allocation are rough around the edges. Rust 1.0 shipped in 2015, it's certainly no where near as mature as C++ (though it's gaining ground quickly, and can obviously benefit from lessons learned in C++).
Also note that Rust's approach is to have many things that would be core/stdlib features in other languages implemented in community supported packages, where a consensus can form safely without risking shipping a language feature that might not serve the needs of the community, and the package can evolve on it's own schedule & not be tied to a compiler release schedule. On a cursory glance, there are numerous libraries providing this functionality: https://crates.io/search?q=placement%20new
I'm curious, are you a game developer or something? I've written less than 1k lines of C++, so I really don't know much about it, but I was under the impression this was a fairly niche feature.
(ETA: I peaked at your profile & saw it looks like you work at a brokerage - yeah, that makes sense why you'd care deeply about avoiding allocations, fair enough.)
Placement new is kind of niche but it's not as rare as you might think, especially if you consider cases where you're using it through some other abstraction. For example, in C++ std::shared_ptr is implemented as the the object you're pointing to, plus another data structure called a control block that contains the reference count for the pointed-to data. The control block has to be heap allocated. If you're allocating a shared object, you could allocate the object being pointed to, and then also allocate the control block... but that requires two allocations. That's why the standard library has std::make_shared, which does a single allocation for both the object being pointed to as well as the control block (there's also a secondary benefit which is that the control block is adjacent in memory to the data it points to, which improves memory locality and caching). To implement std::make_shared you need to use placement new, as you're doing one allocation and two initializations.
I write C++ as my day job and while I wouldn't say I have to write placement new code often (I don't think I've used it once in the past year), it's definitely an essential tool for writing certain abstractions.
For sure. That's interesting, thank you for sharing. In Rust an Arc<T>, the equivalent to shared_ptr, would also require a single contiguous allocation, because it's size is the size of T plus the size of the Arc's internal state. But I must be missing something because C++ has had templates since more then a decade before Rust 1.0 released. I suspect the difference is in how object lifetimes are handled & Rust's lack of a language-level concept of a constructor or a new keyword.
Skimming the source code for Arc<T>, it looks like T will likely be on the stack & copied to the heap, adding a copy C++ is probably able to avoid with it's strategy, but I'm not sure if the compiler is able to optimize that out or not.
> I suspect the difference is in how object lifetimes are handled & Rust's lack of a language-level concept of a constructor or a new keyword.
Object lifetimes certainly play a role. The data structure used by the shared pointer can be kept alive by a weak pointer, but you want to call the destructor of the embedded object when no more shared pointers point to it. If you just used a templated member the auto generated cleanup code would try to call the destructor when the data structure itself is disposed. That is too late if weak_ptrs where involved and you can't call the destructor manually before that without causing loads of FUN as the automated cleanup will try to destroy the object again later. With placement new the member object can be created and destroyed as needed without the language imposing any assumptions on the objects lifetime.
The proposal has received a lot of new discussion in the past few days, as in addition to being wanted by kernel developers, it's a potential path to support dynamic dispatch in `async` code.
Rust can do it with "unsafe" pointers, but the safe subset of the language lacks ergonomic and foolproof interface for it.
Ability to witness uninitialized memory is "unsafe" in Rust. A safe mutable reference is required to point to valid initialized object at all times, no cheating.
In practice Rust usually gets away with not having placement new and letting LLVM eliminate copies.
I'm no C++ god, but I use placement new regularly. It's a standard, normal, typical feature in C++ development.