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

I'm looking at Box in std::boxed - Rust

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

4 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!

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