为什么以下操作失败?如何将拥有的指针存储到特征中?
trait Trait {};
struct Struct;
impl Trait for Struct {};
struct Container {
child: ~Trait
};
let container = ~Container { child: ~Struct };
错误:类型不匹配:预期~main::test02::Trait
但找到~main::test02::Struct
(预期特质benchmark::test02::Trait
,但找到~-ptr
)
答案 0 :(得分:4)
你必须施放值:
trait Trait {}
struct Struct;
impl Trait for Struct {}
struct Container {
child : ~Trait
}
fn main() {
let container = ~Container { child: ~Struct as ~Trait };
println("")
}