| ▲ | ninkendo 4 hours ago | |||||||
I literally had `MaybeUninit::uninit().assume_init()` first in the comment but rewrote it with `std::mem::uninitialized()` because I thought it would be more readable to a C++ crowd, but perhaps your right, I should have left it as-is. | ||||||||
| ▲ | tialaramex 4 hours ago | parent [-] | |||||||
MaybeUninit::uninit().assume_init() is, exactly like the deprecated† core::mem::uninitialized() immediately UB unless your type is zero size and thus there's no difference between initialized and uninitialized. You can unsafely ask for an uninitialized value of any type, but you should not ever assert that you've initialized it unless you actually have. Essentially, you can write this in Rust, but you need to be specific about what you're doing and when you are or are not initializing the variable or else things will go badly for you. It wouldn't be worse in C++, historically, but C++ people are more used to their world being set on fire by an angry compiler because they did something wrong. In Rust you'd be quite surprised by the compiler's wrath. † Edited to add: Technically today you do not get this if you call core::mem::uninitialized, because it was so dangerous, instead if you call the deprecated function on a non-ZST - ironically you get a a value scribbled with 0x01 bytes. Doing this ensures in most cases LLVM won't set fire to your program immediately but at a price of doing all that extra work of writing 0x01 bytes. But we did tell you (for several years now) how to do better. | ||||||||
| ||||||||