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.
Question&Answers:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…