Generics 为什么我会得到;缺少生存期说明符“;或;类型参数的数目错误“;在为结构实现特征时?

Generics 为什么我会得到;缺少生存期说明符“;或;类型参数的数目错误“;在为结构实现特征时?,generics,rust,Generics,Rust,我试图为结构定义和实现一个特性。我所有使用泛型和生存期的实现都有问题。这一定是新手犯的错误。我做错了什么 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: &

我试图为结构定义和实现一个特性。我所有使用泛型和生存期的实现都有问题。这一定是新手犯的错误。我做错了什么

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();
}
pub结构点{
x:i32,
y:i32,
}
///纯寿命示例
发布结构Foo1 Bar1 for Foo1 Bar1 for Foo1{
|^^^^预期的生存期参数
错误[E0243]:类型参数的数量错误:应为1,找到0
-->src/main.rs:32:9
|
32 |适用于Foo2的impl Bar2{
|^^^^^应为1个类型参数
错误[E0243]:类型参数的数量错误:应为1,找到0
-->src/main.rs:47:17
|

47 | impl Bar3 for Foo3错误消息对我来说非常清楚。它们指向一个类型,并声明该类型需要一个生存期或一个类型。添加它们:

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> { /* ... */ }

impl for Foo1 for Foo3Perhaps你可以解释你对错误消息感到困惑的地方吗?非常感谢@Shepmaster!现在我明白了我的错误,这是非常明显的。但是我在错误的思维中呆了几个小时,我本该早点问的。我正在尝试编写一个过程宏,所以我必须为它们所要求的任何结构做好准备扔给我。这些东西只是为了测试和显示我的问题在一个可读和集中的方式。再次感谢!
pub trait Bar3<'a, T: 'a> {
//            ^^^^^^^^^^^
    fn baaar(&self);
}