Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does Rust require generic type declarations after the "impl" keyword?

Tags:

rust

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?

like image 332
Nan Xiao Avatar asked Aug 03 '17 02:08

Nan Xiao


1 Answers

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
    }
}
like image 190
Francis Gagné Avatar answered Sep 19 '22 16:09

Francis Gagné