1

为什么以下失败以及如何存储指向特征的拥有指针?

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

4

1 回答 1

4

您必须转换值:

trait Trait {}
struct Struct;
impl Trait for Struct {}

struct Container {
  child : ~Trait
}
fn main() {
    let container = ~Container { child: ~Struct as ~Trait };
    println("")
}
于 2013-05-29T05:32:17.800 回答