简体   繁体   English

预期的混凝土寿命,在结构中存储fn时找到约束寿命参数

[英]Expected concrete lifetime, found bound lifetime parameter when storing a fn in a struct

I'm trying to store a function in a struct: 我正在尝试将函数存储在结构中:

trait T<'a> {}

struct A {}

struct B<'a> {
    a: &'a A
}

impl<'a> T<'a> for B<'a> {}

fn f1<'a, E: T<'a>>(a: &'a A) {}

struct D {
    f: fn(&A)
}

fn main() {
    let d = D { f: f1::<B> };
}

The compiler complains: 编译器抱怨:

error[E0308]: mismatched types
  --> src/main.rs:18:20
   |
18 |     let d = D { f: f1::<B> };
   |                    ^^^^^^^ expected concrete lifetime, found bound lifetime parameter 
   |
   = note: expected type `fn(&A)`
   = note:    found type `fn(&A) {f1::<'_, B<'_>>}`

When you write f1::<B> , the compiler interprets that as f1::<B<'_>> , there '_ is a lifetime inferred by the compiler, because B is generic over a lifetime and you can only pass concrete types as type parameters. 当您编写f1::<B> ,编译器将其解释为f1::<B<'_>> ,编译器会推断出'_的生存期,因为B在整个生存期内都是通用的,您只能传递具体的类型作为类型参数。

But then, in D , the f field is expected to be a function that accepts references to A with any lifetime. 但是,在Df字段应该是一个可以接受任何生存期的对A引用的函数。 f1::<B> does not fulfill that requirement, because the function has been instantiated with a specific lifetime. f1::<B>不满足该要求,因为该函数已使用特定的生存期进行了实例化。

Unfortunately, at the moment, there's no way to make this work. 不幸的是,目前还没有办法使它起作用。 Rust would have to support either higher kinded types or associated type constructors . Rust将必须支持更高种类的类型关联的类型构造函数 You could then define E in f1 to be a type constructor parameter, rather than a type parameter (though I'm wondering how the compiler would handle the 'a lifetime parameter). 然后,您可以在f1中将E定义为类型构造函数参数,而不是类型参数(尽管我想知道编译器将如何处理'a lifetime参数'a )。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 预期的约束寿命参数,找到具体的寿命 - Expected bound lifetime parameter, found concrete lifetime 预期的约束寿命参数,找到的混凝土寿命[E0271] - Expected bound lifetime parameter, found concrete lifetime [E0271] 如何解决:预期的混凝土寿命,但找到了约束寿命参数 - How to fix: expected concrete lifetime, but found bound lifetime parameter StreamExt.scan() 方法上的“预期绑定生命周期参数,找到具体生命周期” - “expected bound lifetime parameter, found concrete lifetime” on StreamExt .scan() method 预期绑定生命周期参数,在尝试传递选项时找到具体生命周期<fnonce></fnonce> - Expected bound lifetime parameter, found concrete lifetime when trying to pass an Option<FnOnce> 锈蚀寿命误差预期具体寿命但发现约束寿命 - Rust lifetime error expected concrete lifetime but found bound lifetime 函数引用:预期的约束生命周期参数,找到具体的生命周期[E0271] - Function references: expected bound lifetime parameter , found concrete lifetime [E0271] 从闭包填充集合时,键入不匹配“绑定生命周期参数”与“具体生命周期” - Type mismatch “bound lifetime parameter” vs “concrete lifetime” when filling a collection from a closure 当使用参数的生命周期作为特征参数时,“预期的关联类型,找到`u32`” - “Expected associated type, found `u32`” when using the lifetime of a parameter as trait parameter in where bound 尝试调用泛型函数时出现“预期的绑定生命周期参数”错误 - “expected bound lifetime parameter” error when attempting to call a generic function
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM