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

后端 未结 1 532
小蘑菇
小蘑菇 2020-11-27 06:36

What is the implementation for this function:

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

The only function in the docume

相关标签:
1条回答
  • 2020-11-27 07:23

    Dereference the value:

    fn unbox<T>(value: Box<T>) -> T {
        *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 speciality 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".

    0 讨论(0)
提交回复
热议问题