我有一个Command<P>具有两个函数的特征,如下所示:
trait Client<P> {}
trait Command<P> {
fn help(&self) -> String;
fn exec(&self, client: &dyn Client<P>) -> String;
}
struct ListCommand {}
impl<P> Command<P> for ListCommand {
fn help(&self) -> String {
return "This is helptext".to_string();
}
fn exec(&self, client: &dyn Client<P>) -> String {
self.help()
}
}
fn main() {
println!("Hello!");
}
Run Code Online (Sandbox Code Playgroud)
Rust 抱怨我无法调用self.help()并exec()出现以下错误:
error[E0282]: type annotations needed
--> src\main.rs:15:14
|
15 | self.help()
| ^^^^ cannot infer type for type parameter `P` declared on the trait `Command`
Run Code Online (Sandbox Code Playgroud)
如何指定调用方法的类型注释Self?
我可以想到三种方法:
Command::<P>::help(self)<Self as Command<P>>::help(self)(或ListCommand代替Self)(self as &dyn Command<P>).help()(我想知道是否有不涉及的变体dyn。)| 归档时间: |
|
| 查看次数: |
117 次 |
| 最近记录: |