How do I get an owned value out of a `Box`?

RustUnsafe

Rust Problem Overview


What is the implementation for this function:

fn unbox<T>(value: Box<T>) -> T {
    // ???
}

The only function in the documentation that looks like what I want is Box::into_raw. The following will type check:

fn unbox<T>(value: Box<T>) -> T {
    *value.into_raw()
}

This gives the error error[E0133]: dereference of raw pointer requires unsafe function or block. Wrapping it in an unsafe { ... } block fixes it.

fn unbox<T>(value: Box<T>) -> T {
    unsafe { *value.into_raw() }
}

Is this the correct implementation? If so, why is it unsafe? What does it mean?

Perhaps this question shows my general uncertainty of how Boxs actually work.

Rust Solutions


Solution 1 - Rust

Dereference the value:

fn unbox<T>(value: Box<T>) -> T {
    *value
}

There's a nightly associated function into_inner you can use as well:

#![feature(box_into_inner)]
fn unbox<T>(value: Box<T>) -> T {
    Box::into_inner(value)
}

Way back in pre-1.0 Rust, heap-allocated values were very special types, and they used the sigil ~ (as in ~T). Along the road to Rust 1.0, most of this special-casing was removed... but not all of it.

This particular specialty goes by the name "deref move", and there's a proto-RFC about supporting it as a first-class concept. Until then, the answer is "because Box is special".

See also:

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionCalebmerView Question on Stackoverflow
Solution 1 - RustShepmasterView Answer on Stackoverflow