Matching a generic parameter to an associated type in an impl

后端 未结 3 1540
眼角桃花
眼角桃花 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:43

    You could get rid of the generic argument B and instead of constraining B, directly pass A::Foo as the second generic argument to Baz, but I'm not sure if your actual problem matches the simplified example you showed.

    impl Baz {
        fn addFoo(&mut self)  {
            self.vec.push(self.generator.generate());
        }
    }
    

提交回复
热议问题