Defining the methods of generic type requires adding generic types after impl
:
struct GenericVal<T>(T,);
impl <T> GenericVal<T> {}
I feel that removing <T>
seems OK:
struct GenericVal<T>(T,);
impl GenericVal<T> {}
Is it any special consideration?
Rust allows you to write
impl
blocks that apply only to some specific combination of type parameters. For example:Here, the type
GenericVal
is generic, but theimpl
itself is not.Thus, if you want to write an
impl
block that applies for allGenericVal<T>
types, you must first declare a type parameter on theimpl
itself (otherwise,T
would try to look up a type namedT
).This declaration also lets you have a single type parameter that can be used multiple times, forcing the types to be the same.