特質我有一個功能的特點,這並不需要self
作爲參數:調用靜態方法從泛型類型
trait MyTrait {
fn function(x: i32) -> i32;
}
struct Dummy;
impl MyTrait for Dummy {
fn function(x: i32) -> i32 {
x * 2
}
}
fn call_method<T: MyTrait>(object: T) {
let x = object.function(2);
}
fn main() {}
庫的用戶需要實現的特質對任何類型的,通常一個空的結構。我的一個函數接受一個實現MyTrait
的泛型類型。當我嘗試調用function
方法的泛型類型,它給了我這個錯誤:
error: no method named
function
found for typeT
in the current scope
我試圖在回答解決this question,但我得到了同樣的錯誤。我如何調用泛型類型的靜態方法?