Rust doesn't properly compile the impl code

I don't understand what is so wrong about this code:

struct A {
    a: Vec<u64>,
}

impl<Vec<u64>> std::ops::Deref for A {
    type Target = Vec<u64>;
    
    fn deref(&self) -> &Target {
        &self.a
    }
}

Playground
Exactly the same code but without double < and > like impl<u64> Deref for A {} would work, but here it simply doesn't.

Am I missing something? Thanks.

You should use impl std::ops::Deref for A instead. If you write impl<u64> Deref for A {}, then you introduce a new type parameter called u64. This shadows the u64 primitive type. impl <T> ... should only be used when the impl is generic. For example impl<T> MyType<T> { fn foo() {} } adds a method foo to MyType<T> for all types T. impl MyType<bool> { fn foo() {} } only adds it to the specific type MoType<bool>.

1 Like