Skip to content

Doc clarification for MaybeUninit::zeroed() #111608

Description

@danielocfb

Location

https://doc.rust-lang.org/std/mem/union.MaybeUninit.html#method.zeroed

Creates a new MaybeUninit in an uninitialized state, with the memory being filled with 0 bytes. It depends on T whether that already makes for proper initialization. For example, MaybeUninit::zeroed() is initialized, but MaybeUninit<&'static i32>::zeroed() is not because references must not be null.

Summary

While perhaps implied by the terminology of "memory being filled with 0 bytes", it would be great to explicitly state whether or not padding bytes are included.

For me it's a bit ambiguous because both std::mem::zeroed and std::mem::MaybeUninit are parametrized by T. Hence, neither really exposes a concept of "raw" bytes to the user, but just the Rust type. Yet if I compare the above documentation to std::mem::zeroed it states, among other things:

This means that, for example, the padding byte in (u8, u16) is not necessarily zeroed.

So there padding bytes are explicitly excluded.

It's particularly confusing, because std::mem::zeroed states:

This has the same effect as MaybeUninit::zeroed().assume_init(). It is useful for FFI sometimes, but should generally be avoided.

Should we make it more explicit that std::mem::MaybeUninit::zeroed also zeroes out padding bytes? (assuming that's the behavior we want; it seems to be the behavior we currently have)

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

A-docsArea: Documentation for any part of the project, including the compiler, standard library, and toolsE-help-wantedCall for participation: Help is requested to fix this issue.

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions