# Re-use struct fields on drop; was: Drop(&mut self) vs. drop(self)

**URL:** https://internals.rust-lang.org/t/re-use-struct-fields-on-drop-was-drop-mut-self-vs-drop-self/8594
**Category:** language design
**Created:** [October 17, 2018, 10:16am UTC](https://internals.rust-lang.org/t/re-use-struct-fields-on-drop-was-drop-mut-self-vs-drop-self/8594 "2018-10-17T10:16:36Z")
**Posts on this page:** 1
**Showing post:** 23

<div class="post-metadata">

### Author: ![scottmcm](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/scottmcm/32/2355_2.png) [@scottmcm](https://internals.rust-lang.org/u/scottmcm)
#### Post date: [October 18, 2018, 8:04am UTC](https://internals.rust-lang.org/t/re-use-struct-fields-on-drop-was-drop-mut-self-vs-drop-self/8594/23 "2018-10-18T08:04:19Z")

</div>

> [@literda](#):
>
> I don’t think that `ManuallyDrop` allows me to do what [I would like](https://internals.rust-lang.org/t/re-use-struct-fields-on-drop-was-drop-mut-self-vs-drop-self/8594/3). I can simply not use `ManuallyDrop::into_inner(slot: ManuallyDrop<T>) -> T` from a `drop(&mut self)` because I can not move `slot` .

Just `ptr::read` it:

```rust
impl Drop for Guard {
  fn drop(&mut self) {
    let resource = unsafe { ManuallyDrop::into_inner(ptr::read(&self.resource)) };
    do_something_more_with_precious_resource(resource);
  }
}

```

you shouldn't then read it _again_, but since the only place you're doing this is in `drop`, that's easy.

(It might be worth having an `unsafe` method for this combination specifically, to make it more obvious.)

> [@CAD97](#):
>
> ```rust
> impl Drop for Guard {
> fn drop(&mut self) {
> let mut out = unsafe { mem::uninitialized() };
> mem::swap(&mut out, &mut *self.resource);
> do_something_more_with_precious_resource(out)
> }
> }
> 
> ```
> 
> (This could also potentially do ptr::copy instead of mem::swap but I did the easy option)

Please treat `mem::uninitialized()` as deprecated; it will be soon. And `ptr::read` is simpler and better here.

(Also, reminder that "swap with a local" is usually better written with `mem::replace`.)

> [@literda](#):
>
> Yes. And while it works well, it still itches because it requires `unsafe` . What’s your opinion about adding support for this [kind of thing](https://internals.rust-lang.org/t/re-use-struct-fields-on-drop-was-drop-mut-self-vs-drop-self/8594/3) to the compiler?

The attribute-and-stringly-connected version feels like too much of a hack for me to have great confidence in it getting accepted. I think it would have better luck if it was more like normal behaviour, just a bit special since `Drop` is already special.

Imagine, say, that you could do something like this:

```rust
impl DropPrime for Guard {
  fn drop_prime(Guard { resource }: Self) {
    do_something_more_with_precious_resource(resource);
  }
}

```

That's already legal syntax. It correctly drops all the fields if you don't put anything in the body, but it gives you ownership of them to move or forget as you wish. It wouldn't even need the "you can't call `Drop::drop` manually" restriction. (Just a separate ad-hoc restriction of "you must destructure in the parameter-pattern".)

---

_[View the full topic](https://internals.rust-lang.org/t/re-use-struct-fields-on-drop-was-drop-mut-self-vs-drop-self/8594)._
