When I try to call the function method on a generic type, ...
Two things: first of all, depending on how you want to look at it, function not a method. This is just a function that lives in the trait namespace (aka "related function").
Secondly, you are not trying to call function on a type, you are calling it on a value of that type. This is impossible, because, again, this is not a method; it does not have a self parameter.
The solution is to actually call the associated function function in the generic type, which looks like this:
fn call_method<T: MyTrait>(object: T) { let x = T::function(2); }
Sometimes this will not be specific enough. If you need to be more specific, you can also write above:
fn call_method<T: MyTrait>(object: T) { let x = <T as MyTrait>::function(2); }
The two are semantically identical; it is simply that the second is more specific and more likely to be resolved when you have a lot of traits.
source share