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:
struct GenericVal<T>(T);
impl GenericVal<u32> {
fn foo(&self) {
// method foo() is only defined when T = u32
}
}
Here, the type GenericVal
is generic, but the impl
itself is not.
Thus, if you want to write an impl
block that applies for all GenericVal<T>
types, you must first declare a type parameter on the impl
itself (otherwise, T
would try to look up a type named T
).
struct GenericVal<T>(T);
impl<T> GenericVal<T> {
fn foo(&self) {
// method foo() is always present
}
}
This declaration also lets you have a single type parameter that can be used multiple times, forcing the types to be the same.
struct GenericVal<T, U>(T, U);
impl<V> GenericVal<V, V> {
fn foo(&self) {
// method foo() is only defined when T = U
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With