[][src]Crate safe_uninit

Goal of the crate is to allow using safe uninitialized values. Many similar features are already in the Rust core library as MaybeUninit and functions in standard types. For example, currently (stable Rust 1.41) as nightly feature of a compiler one can see Box::new_uninit() which allows to allocate memory with MaybeUninit value. Basically, this new crate allows creating not 'Maybe' but surely uninitialized values that are safe to use despite they are uninitialized. Because of this they are directly presented as a value without any wrappers like MaybeUninit and no requirement for unsafe block.

Main trait is SafeUninit which indicated the type which can be safely used without initialization and without further wrappers. It is implemented for all primitive integer types and their atomic variants, for fixed-size arrays of SafeUninit of up to 32 values (but there is a way of creating bigger arrays), for tuples of SafeUninit objects of up to 12 elements and for unit type ().

This crate is no-std but also implements traits for alloc types where appropriate.


Pointers are safe to be uninitialized. Even if the values they are pointing to are not SafeUninit. Firstly, pointers are internally a plain number of type usize which is safe. Secondly, dereferencing pointers is an unsafe operation anyway and even if pointer with uninitialized address gets dereferenced this will be done under unsafe block and programmer will be fully responsible for any consequences of using it.

Common Types That are Unsafe

These types are not safe to use uninitialized and one should use MaybeUninit instead.


Boolean valid values are true and false. If boolean is internally (as an example) stored as a byte which holds values different from 0 or 1 then this will lead to unexpected behaviour and thus this type is not safe to use uninitialized. One should use MaybeUninit for bool.


Such types as NonZeroI32 are unsafe to leave uninitialized. These types are assumed to never be zero. Uninitialized value though can occur zero and this will cause undefined behaviour.

Might be Unsafe

Here are listed types that can be unsafe and should be further investigated:

  • char
  • f32
  • f64



To be used with Vec-like types. Adds Vec a capability to resize it's content while leaving new values uninitialized.


Marks the type that is safe to use uninitialized. For example, if you create uninitialized u32 you still can use it and it would not cause any damage.


Similar to SafeUninit. This trait intended to be implemented for types like Rc or Box and instead mean that content that this object holds inside is uninitialized (and not Rc or Box itself).



Shorthand for types that are SafeUninit.

Derive Macros