Creates a new `MaybeUninit<T>` in an uninitialized state???

The generic T is associated with the return value of the uninit method in the red area in the figure below, but not in the internal implementation (MaybeUninit { uninit: () }), how is it associated with T, and the size of T is indeterminate, why is it designed that way?

This is a union, so it can be either the uninit field or the value field, and it can be changed at any time, as permitted by union. Look at the new method.

Another question in the red area in the picture below, I didn't find the mem directory or file in the std directory when I was looking through the rust source code, how is it related?
And I can't understand how the is associated here, can you explain it in detail?

Screenshot_select-area_20221222221430

The std crate gets many of its modules from the core crate. mem is defined in core::mem, in library/core/src/mem/mod.rs.

The MaybeUninit::<u32> is the turbofish syntax. Here <u32> isn't a module, but a type argument for MaybeUninit<u32>, but in expressions it needs extra :: to disambiguate it from less than operator.

4 Likes

In case you (or other readers) weren’t aware: you can customize the links to this website to match the turbofish at hand :wink:.

u32

2 Likes

When I use use std::mem::MaybeUninit, how does it find the library/core/src/mem/mod.rs, what is the whole process or implementation mechanism, can you elaborate?

It's reexported from std here

1 Like

I read the content of the file you sent, but I still have a little bit do not understand which piece in this file for export, export process do not understand you can explain in detail?

The link should have automatically highlighted the line, but it's this one pub use core::mem;