I have a trait with one function that does not take self
as an argument:
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() {}
The user of the library is required to implement the trait for any type, usually an empty struct. One of my functions accepts a generic type that implements MyTrait
. When I try to call the function
method on the generic type, it gives me this error:
error: no method named
function
found for typeT
in the current scope
I tried the solution in the answer to this question, but I get the same error. How can I call a static method on a generic type?