Why do I get “missing lifetime specifier” or “wrong number of type arguments” when implementing a trait for a struct?

前端 未结 1 2015
無奈伤痛
無奈伤痛 2020-12-21 18:36

I\'m trying to define and implement a trait for a struct. All my implementations with generics and lifetime have problems. This must be a rookie mistake. What am I doing wro

相关标签:
1条回答
  • 2020-12-21 18:48

    The error messages seem pretty clear to me. They point at a type and state that the type needs a lifetime or a type. Add them:

    impl<'a> Bar1<'a> for Foo1<'a> { /* ... */ }
    impl<T> Bar2<T> for Foo2<T> { /* ... */ }
    impl<'a, T: 'a> Bar3<'a, T> for Foo3<'a, T> { /* ... */ }
    

    This is required because you've created parameterized traits:

    pub trait Bar3<'a, T: 'a> {
    //            ^^^^^^^^^^^
        fn baaar(&self);
    }
    

    None of the traits you have defined need any kind of generic parameters, so the "real" solution is just to remove them. I assume you've added them for some learning purpose not shown here though.

    0 讨论(0)
提交回复
热议问题