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 Box
s actually work.
Box
. – Enterotomycore::ptr::read
method. – Enterotomyptr::read
here). – Saritasarkaria