作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个带有函数成员的结构:
struct Foo<T> {
fun: Box<dyn Fn(T)>,
}
type FooI = Foo<&mut i32>;
error[E0106]: missing lifetime specifier
--> src/main.rs:5:17
|
5 | type FooI = Foo<&mut i32>;
| ^ expected named lifetime parameter
|
help: consider introducing a named lifetime parameter
|
5 | type FooI<'a> = Foo<&'a mut i32>;
最佳答案
不幸的是,您要尝试执行的操作目前无法在Rust上使用。当某个结构在引用上是通用的时,该引用至少在该结构的生命期内必须存在。因此,如果您收听编译器,它将无法执行您想要的操作:
playground link
因此,您要么必须将fun
设置为Box<dyn Fn(&mut T)>
,要么将您的结构更改为通用。
struct Foo<T> {
fun: Box<dyn Fn(&mut T)>
}
type FooI = Foo<i32>;
关于rust - Rust:Fn成员签名中使用的Struct泛型类型参数需要命名生命周期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62227107/
我是一名优秀的程序员,十分优秀!