External Publication
Visit Post

Another Experiment To Make Unsafe Rust Safer: Preventing UB In MaybeUninit With Compile Time Error

Rust Internals [Unofficial] May 22, 2026
Source

MaybeUninit<T> is a union (a tagless enum) between a dataless variant called uninit and a variant with T data called value.

Option<T> is an enum between a dataless variant called None and a variant with T data called Some.

In other words, (ignoring optimizations and focusing on semantics,) Option<T> is very similar to a (bool, MaybeUninit<T>) pair, where the boolean tag indicates whether the second field is initialized. The initialization is checked at runtime instead of compile time.

With typestate, you have two separate types, one which is dataless and one which has T data. This is already possible with () and T respectively, even if wrappers around MaybeUninit<T> could serve the same purpose.

Incremental initialization could be interesting.

Discussion in the ATmosphere

Loading comments...