Why does Rust require generic type declarations af

2020-02-12 04:43发布

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
1条回答
Animai°情兽
2楼-- · 2020-02-12 05:26

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
    }
}
查看更多
登录 后发表回答