问题描述
在我的代码中,我有一些实现Trait1的对象。我还具有实现Trait2的各种对象,该对象期望将Trait1对象作为参数。当我尝试创建满足Trait2的对象矢量时,出现编译器错误。
我可以得到的最简单的复制方式:
trait Trait1 {
fn doit(&self);
}
trait Trait2 {
fn do_other_thing(&self,obj: &impl Trait1);
}
fn main() {
let mut myvec = Vec::<Box<dyn Trait2>>::new();
}
这会导致编译器错误:
error[E0038]: the trait `Trait2` cannot be made into an object
--> src/main.rs:10:18
|
5 | trait Trait2 {
| ------ this trait cannot be made into an object...
6 | fn do_other_thing(&self,obj: &impl Trait1);
| -------------- ...because method `do_other_thing` has generic type parameters
...
10 | let mut myvec = Vec::<Box<dyn Trait2>>::new();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `Trait2` cannot be made into an object
|
= help: consider moving `do_other_thing` to another trait
有什么办法可以实现这样的目标吗? “ myvec”必须在Trait2上具有动态性,因为我希望持有实现此特征的不同类型。有什么方法可以允许'obj'在Trait1上完全通用,还是我必须将其绑定到实现Trait1的特定类型?
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)