# mini-RFC: ManuallyDrop::take

**URL:** <https://internals.rust-lang.org/t/mini-rfc-manuallydrop-take/8679>\
**Category:** Uncategorized\
**Created:** [October 26, 2018, 2:09am UTC](https://internals.rust-lang.org/t/mini-rfc-manuallydrop-take/8679 "2018-10-26T02:09:20Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![CAD97](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/cad97/32/3460_2.png) [@CAD97](https://internals.rust-lang.org/u/CAD97)\
**Post date:** [October 26, 2018, 2:09am UTC](https://internals.rust-lang.org/t/mini-rfc-manuallydrop-take/8679/1 "2018-10-26T02:09:20Z")

</div>

Prompted by all of the `Drop` discussion, and quoting [@scottmcm’s implementation](https://internals.rust-lang.org/t/re-use-struct-fields-on-drop-was-drop-mut-self-vs-drop-self/8594/23?u=cad97).

If there aren’t any objections, suggestions, or corrections, I’ll probably open a PR with this. It seems like a small but useful addition. I’m primarily interested in suggestions for wording of the docs.

```rust
impl<T> ManuallyDrop<T> {
    
    /// Take the contained value out.
    ///
    /// This method is primarily intended for moving out values in drop.
    /// Instead of using `ManuallyDrop::drop` to manually drop the value,
    /// you can use this method to take the value and use it however desired.
    /// `Drop` will be invoked normally at end of scope, as with all other owned values.
    ///
    /// If you have ownership of the value, you can use `ManuallyDrop::into_inner` instead.
    ///
    /// # Safety
    ///
    /// This function semantically moves out the contained value without preventing further usage.
    /// It is up to the user of this method to ensure that this container is not used again.
    pub unsafe fn take(slot: &mut ManuallyDrop<T>) -> T {
        ManuallyDrop::into_inner(ptr::read(slot))
    }
}

```

---

<div class="post-metadata">

**Author:** ![earthengine](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/earthengine/32/2577_2.png) [@earthengine](https://internals.rust-lang.org/u/earthengine)\
**Post date:** [October 26, 2018, 4:11am UTC](https://internals.rust-lang.org/t/mini-rfc-manuallydrop-take/8679/2 "2018-10-26T04:11:14Z")

</div>

Is it necessory to add a `ManuallyDrop::drop()` call?

```rust
impl<T> ManuallyDrop<T> {
    
    /// Take the contained value out.
    ///
    /// This method is primarily intended for moving out values in drop.
    /// Instead of using `ManuallyDrop::drop` to manually drop the value,
    /// you can use this method to take the value and use it however desired.
    /// `Drop` will be invoked normally at end of scope, as with all other owned values.
    ///
    /// If you have ownership of the value, you can use `ManuallyDrop::into_inner` instead.
    ///
    /// # Safety
    ///
    /// This function semantically moves out the contained value without preventing further usage.
    /// It is up to the user of this method to ensure that this container is not used again.
    pub unsafe fn take(slot: &mut ManuallyDrop<T>) -> T {
        let r = ManuallyDrop::into_inner(ptr::read(slot))
        ManuallyDrop::drop(slot);
        r
    }
}

```

---

<div class="post-metadata">

**Author:** ![CAD97](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/cad97/32/3460_2.png) [@CAD97](https://internals.rust-lang.org/u/CAD97)\
**Post date:** [October 26, 2018, 4:14am UTC](https://internals.rust-lang.org/t/mini-rfc-manuallydrop-take/8679/3 "2018-10-26T04:14:27Z")

</div>

No? Why would it be? (It would be unsound to include?)

The whole point is to take the value _out_ of the `ManuallyDrop`, _without_ dropping it, so that the caller can have the value and do whatever with it (which will (probably) include dropping it at some point in the future). The version left behind in `slot` is effectively uninitialized memory at this point.

---

<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 26, 2018, 5:19am UTC](https://internals.rust-lang.org/t/mini-rfc-manuallydrop-take/8679/4 "2018-10-26T05:19:36Z")

</div>

Looks good! One minor idea: add the following to it:

```rust
#[must_use = "if you don't need the value, you can `ManuallyDrop::drop` instead"]

```

---

<div class="post-metadata">

**Author:** ![CAD97](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/cad97/32/3460_2.png) [@CAD97](https://internals.rust-lang.org/u/CAD97)\
**Post date:** [October 27, 2018, 7:19pm UTC](https://internals.rust-lang.org/t/mini-rfc-manuallydrop-take/8679/5 "2018-10-27T19:19:42Z")

</div>

PR posted: [https://github.com/rust-lang/rust/pull/55421](https://github.com/rust-lang/rust/pull/55421)

---

<div class="post-metadata">

**Author:** ![system](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/system/32/14092_2.png) [@system](https://internals.rust-lang.org/u/system)\
**Post date:** [March 25, 2019, 8:17am UTC](https://internals.rust-lang.org/t/mini-rfc-manuallydrop-take/8679/6 "2019-03-25T08:17:02Z")

</div>

Closed on March 25, 2019, 8:17am UTC

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.
