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?


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

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.


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



When I use use std::mem::MaybeUninit, how does it find the library/core/src/mem/, 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;

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.