Skip to content

ptr::read docs are bogus #21491

Closed
Closed
@kmcallister

Description

@kmcallister

Reads the value from src without dropping it.

But in fact this code does run the destructor:

use std::{ptr, mem};

struct Foo;

impl Drop for Foo {
    fn drop(&mut self) {
        println!("drop day");
    }
} 

fn main() {
    let x = Foo;
    unsafe {
        ptr::read(&x as *const Foo);
        mem::forget(x);
    }
} 

It's not clear what "without dropping it" means. I suspect the point is that it won't mark the target (hence *const T) as destroyed, from an era when this was more of a dynamic notion than today. But at any rate, the docs should reflect reality. As far as I know this is the way to explicitly run a destructor in Rust — a nice duality because ptr::write is the "placement new" for uninitialized memory.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions