为什么我会收到“缺少生命周期说明符"?或“类型参数数目错误";在实现结构特征时? [英] Why do I get "missing lifetime specifier" or "wrong number of type arguments" when implementing a trait for a struct?

查看:53
本文介绍了为什么我会收到“缺少生命周期说明符"?或“类型参数数目错误";在实现结构特征时?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为结构定义和实现特征.我所有带有泛型和生命周期的实现都有问题.这一定是菜鸟的错误.我在做什么错了?

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();
}

编译器结果

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

推荐答案

错误消息对我来说似乎很清楚.他们指向一个类型,并指出该类型需要生存期或类型.添加它们:

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.

这篇关于为什么我会收到“缺少生命周期说明符"?或“类型参数数目错误";在实现结构特征时?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆