trait Foo {}
trait Bar: Foo {}
struct Zxc;
impl Foo for Zxc {}
struct Baz;
impl Bar for Baz {}
impl Foo for Baz {}
struct Abc<F: Foo> {
f: F
}
impl<F: Foo> Abc<F> {
fn bared<B: Bar>(&mut self, b: B) {
self.f = b;
}
}
fn main() {
let mut abc = Abc { f: Zxc };
abc.bared(Baz);
}
Try it on the playground中保存`trait Bar:Foo {}`。在`struct Abc'
Abc
存储Foo
特征; abc.bared(Baz)
采取Baz
,实现Foo
和Bar
,但在Abc
中保存Baz
时存在类型不匹配错误。如何解决它?
可能像使用'unsafe'? – Lupe
我会100%推荐不要在这里使用'unsafe'。我不相信这是必要的,使用它很可能会导致内存错误。 – Shepmaster
@Shepmaster拐杖创造漂亮的API。 – Lupe