【问题标题】:Lifetime subtyping and generics: "lifetime parameter not allowed" error生命周期子类型和泛型:“生命周期参数不允许”错误
【发布时间】:2018-09-19 16:28:34
【问题描述】:

我尝试关注strategy described in the book(和编译器),但遇到以下错误:

lifetime parameters are not allowed on this type: lifetime parameter not allowed

这里是sn-p的代码:

struct FooRef<'a, F: 'a>(&'a F);

struct Bar<'a, 'f: 'a, F: Foo<'f>> {
    filter: &'a FooRef<'f, F<'f>>,
}

【问题讨论】:

标签: generics rust lifetime


【解决方案1】:

F 是类型变量,而不是类型,因此不能有自己的类型或生命周期参数。如果您需要将类型变量限制为仅保存受生命周期限制的引用,则可以使用生命周期作为边界:

struct Bar<'a, 'f: 'a, F: Foo<'f> + 'f> {
    filter: &'a FooRef<'f, F>,
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-05-24
    • 2023-02-10
    • 2018-12-09
    • 2019-04-08
    • 1970-01-01
    • 2014-09-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多