小编Pow*_*nda的帖子

调用非泛型特征方法时的泛型类型参数

我有一个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` …
Run Code Online (Sandbox Code Playgroud)

traits rust

5
推荐指数
1
解决办法
117
查看次数

标签 统计

rust ×1

traits ×1