Matching a generic parameter to an associated type in an impl

后端 未结 3 1534
眼角桃花
眼角桃花 2020-11-29 09:40

I have a trait with an associated type and a generic struct::

trait Generator {
    type Foo;
    fn generate(&self) -> Self::Foo;
}

struct Baz

        
3条回答
  •  迷失自我
    2020-11-29 09:52

    I must explain to the compiler that B is the same as A::Foo

    There is a special syntax for it:

    impl Baz
    where
        A: Generator,
    {
        fn add_foo(&mut self) {
            self.vec.push(self.generator.generate());
        }
    }
    

提交回复
热议问题