Is there no Box<T> -> T function?

I'm looking at https://doc.rust-lang.org/std/boxed/struct.Box.html

I can't seem to find a Box<T> -> T function.

Intuitively, this function should exist since it's just "moving object from Heap to Stack".

Does this function exist? If not, why?

*my_box

3 Likes

assuming the contents are Sized, a dereference can be used to move something out of the box

so this works

fn main() {
    let boxed = Box::new("test".to_owned());
    let unboxed = *boxed;
}

but not

struct A {}
trait T {}
impl T for A {}

fn main() {
    let boxed: Box<dyn T> = Box::new(A {});
    let unboxed = *boxed;
}
1 Like

deref worked. Thanks!