Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
698 views
in Technique[技术] by (71.8m points)

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

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 wrong?

main.rs

pub struct Point {
    x: i32,
    y: i32,
}

/// pure lifetime example
pub struct Foo1<'a> {
    pub first_attribute: u32,
    pub second_attribute: Point,
    third_attribute: &'a [Point],
}

pub trait Bar1<'a> {
    fn baaar();
}

impl<'a> Bar1 for Foo1<'a> {
    fn baaar() {}
}

///pure type example
pub struct Foo2<T> {
    pub first_attribute: u32,
    pub second_attribute: Point,
    third_attribute: [T],
}

pub trait Bar2<T> {
    fn baaar(&self);
}

impl<T> Bar2 for Foo2<T> {
    fn baaar(&self) {}
}

/// real world example
pub struct Foo3<'a, T: 'a> {
    pub first_attribute: u32,
    pub second_attribute: Point,
    third_attribute: &'a [T],
}

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

impl<'a, T: 'a> Bar3 for Foo3<'a, T> {
    fn baaar(&self) {}
}

fn main() {
    let x = Point { x: 1, y: 1 };
    let c = Foo3 {
        first_attribute: 7,
        second_attribute: Point { x: 13, y: 17 },
        third_attribute: &x,
    };

    c.baaar();
}

compiler results

error[E0106]: missing lifetime specifier
  --> src/main.rs:17:10
   |
17 | impl<'a> Bar1 for Foo1<'a> {
   |          ^^^^ expected lifetime parameter

error[E0106]: missing lifetime specifier
  --> src/main.rs:47:17
   |
47 | impl<'a, T: 'a> Bar3 for Foo3<'a, T> {
   |                 ^^^^ expected lifetime parameter

error[E0243]: wrong number of type arguments: expected 1, found 0
  --> src/main.rs:32:9
   |
32 | impl<T> Bar2 for Foo2<T> {
   |         ^^^^ expected 1 type argument

error[E0243]: wrong number of type arguments: expected 1, found 0
  --> src/main.rs:47:17
   |
47 | impl<'a, T: 'a> Bar3 for Foo3<'a, T> {
   |                 ^^^^ expected 1 type argument
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...