这个简单的程序:
fn main() {
let b: Box<i32> = Box::new(1);
b.into_raw();
}
使用 Rust 1.12.0 编译时,会产生这种不便的错误:
error: no method named `into_raw` found for type `Box<i32>` in the current scope
--> <anon>:3:7
|
3 | b.into_raw();
| ^^^^^^^^
|
= note: found the following associated functions; to be used as methods, functions must have a `self` parameter
= note: candidate #1 is defined in an impl for the type `Box<_>`
这是因为
into_raw
没有定义为带有self
参数的函数,而是被定义为:impl Box<T: ?Sized> {
fn into_raw(b: Box<T>) -> *mut T;
}
这似乎很不方便,我找不到理由。
那么...为什么呢?
Defer
转发给 pointee 的。 - Matthieu M.