rust-lang / nomicon

The Dark Arts of Advanced and Unsafe Rust Programming
https://doc.rust-lang.org/nomicon/
Apache License 2.0
1.74k stars 256 forks source link

std::ptr::read uninitializes T? (Push and Pop) #423

Closed pwbh closed 9 months ago

pwbh commented 9 months ago

So I am reading now the Vec implementation part of the nomicon book and I can't understand how does the Drop trait is called for T's, e.g. how does std::ptr::read leaves the memory logically uninitialized, I am not able to find any documentation to backup this statement, and what would "logically unintialized" mean?

From the book

For this we need ptr::read, which just copies out the bits from the target address and interprets it as a value of type T. This will leave the memory at this address logically uninitialized, even though there is in fact a perfectly good instance of T there.

After I read the ptr from that specific memory address, am I able to read it again as its only creating copy bits aligned for T and if so, where does the "unitialization" happens or under which circumstances?

All the documentation that there is for std::ptr::read is

Reads the value from src without moving it. This leaves the memory in src unchanged.

pwbh commented 9 months ago

Ok actually the documentation does explain this - https://doc.rust-lang.org/std/ptr/fn.read.html#ownership-of-the-returned-value

Closed.