6 ms·
There's no placement new IIRC so you always build on the stack and copy it to the heap. https://users.rust-lang.org/t/how-to-create-large-objects-directly-in-h
by nivenkos 5y ago
There's no placement new IIRC so you always build on the stack and copy it to the heap.
https://users.rust-lang.org/t/how-to-create-large-objects-directly-in-heap/26405 https://users.rust-lang.org/t/how-to-create-large-objects-di...
- andrewaylett 5y agoI think that's a question of an "insufficiently smart compiler" rather than necessarily something the programmer should be concerned about?
- Koshkin 5y agoThe programmer should certainly be concerned about the compiler being insufficiently smart.
- steveklabnik 5y agoNo, it is a language semantics issue. You can get the compiler to optimize it away today, if you're careful. But that's a bad user experience. You should be able to specifically request this semantic, and be sure it works.
- School-Cotton 5y agoFair point.
- tialaramex 5y agoYou can do "placement new" (Rust has no new operator, but in this context) unsafely with MaybeUninit -- https://doc.rust-lang.org/stable/std/mem/union.MaybeUninit.html https://doc.rust-lang.org/stable/std/mem/union.MaybeUninit.h... Make a MaybeUninit<Doodad> on the heap, initialise it, and then unsafely assume_init() remembering to write your safety justification ("I initialised this, so it's fine") and get a Doodad, on the heap. The reason it isn't mentioned in that 2019 u.r-l.o post is that MaybeUninit wasn't stabilized until mid-2019.