logoalt Hacker News

ninkendo • today at 6:23 PM • 1 reply • view on HN

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.


Replies

tialaramex • today at 7:04 PM

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.