在闭包 Rust 中使用函数指针参数
Use function pointer parameter inside closure Rust
我想创建一个函数 agg
,它使用另一个函数 get_id
作为参数,returns 一个 FnMut
闭包使用get_id
函数。
具体例子:
struct CowRow {
pub id : i32,
}
impl CowRow {
fn get_id(&self) -> i32 { self.id }
}
pub fn agg<F>(col: F) -> Box<FnMut(&CowRow) -> i32>
where F: Fn(&CowRow) -> i32 {
let mut res = 0;
Box::new(move |r| { res += col(&r); return res })
}
fn main() {
let mut cow = CowRow { id: 0 };
let a = agg(CowRow::get_id);
a(&cow);
产生错误:
the parameter type `F` may not live long enough [E0310]
run `rustc --explain E0310` to see a detailed explanation
consider adding an explicit lifetime bound `F: 'static`...
...so that the type `[closure@main.rs:23:14: 23:53 col:F, res:i32]` will meet its required lifetime bounds
这里的想法是我想要一个通用函数,它允许创建在结构中的不同字段上运行的闭包。因此,我的想法是传递一个函数,该函数是结构的 getter 并在闭包中使用它来提取适当的字段。
我尝试了将 'static
添加到 agg
签名的各种组合,但我不确定这到底意味着什么,也不确定它在句法上需要去哪里。此外,我还尝试了一些技术:https://github.com/nrc/r4cppp/blob/master/closures.md 例如将 get_id
方法添加为特征,但也无法使其正常工作。
您的函数的类型参数 F
具有关联的生命周期(就像所有其他类型一样)。但隐含地,函数 Box<FnMut(&CowRow) -> i32>
的 return 值实际上是 Box<FnMut(&CowRow) -> i32 + 'static>
。也就是说,除非您为盒子指定生命周期,否则它会假定其内容可以永远存在。当然,如果 F
只活了 'a
,那么借用检查员就会抱怨。要解决此问题,
强制F
有一个静态生命周期,这样它就可以在盒子里永远存在(playpen):
fn agg<F>(col: F) -> Box<FnMut(&CowRow) -> i32>
where F: Fn(&CowRow) -> i32 + 'static
{
...
}
明确声明 F
有生命周期 'a
并且 Box
(playpen):
fn agg<'a, F>(col: F) -> Box<FnMut(&CowRow) -> i32 + 'a>
where F: Fn(&CowRow) -> i32 + 'a
{
...
}
第二个版本比第一个版本更通用,并将接受更多的闭包作为参数。
我想创建一个函数 agg
,它使用另一个函数 get_id
作为参数,returns 一个 FnMut
闭包使用get_id
函数。
具体例子:
struct CowRow {
pub id : i32,
}
impl CowRow {
fn get_id(&self) -> i32 { self.id }
}
pub fn agg<F>(col: F) -> Box<FnMut(&CowRow) -> i32>
where F: Fn(&CowRow) -> i32 {
let mut res = 0;
Box::new(move |r| { res += col(&r); return res })
}
fn main() {
let mut cow = CowRow { id: 0 };
let a = agg(CowRow::get_id);
a(&cow);
产生错误:
the parameter type `F` may not live long enough [E0310]
run `rustc --explain E0310` to see a detailed explanation
consider adding an explicit lifetime bound `F: 'static`...
...so that the type `[closure@main.rs:23:14: 23:53 col:F, res:i32]` will meet its required lifetime bounds
这里的想法是我想要一个通用函数,它允许创建在结构中的不同字段上运行的闭包。因此,我的想法是传递一个函数,该函数是结构的 getter 并在闭包中使用它来提取适当的字段。
我尝试了将 'static
添加到 agg
签名的各种组合,但我不确定这到底意味着什么,也不确定它在句法上需要去哪里。此外,我还尝试了一些技术:https://github.com/nrc/r4cppp/blob/master/closures.md 例如将 get_id
方法添加为特征,但也无法使其正常工作。
您的函数的类型参数 F
具有关联的生命周期(就像所有其他类型一样)。但隐含地,函数 Box<FnMut(&CowRow) -> i32>
的 return 值实际上是 Box<FnMut(&CowRow) -> i32 + 'static>
。也就是说,除非您为盒子指定生命周期,否则它会假定其内容可以永远存在。当然,如果 F
只活了 'a
,那么借用检查员就会抱怨。要解决此问题,
强制
F
有一个静态生命周期,这样它就可以在盒子里永远存在(playpen):fn agg<F>(col: F) -> Box<FnMut(&CowRow) -> i32> where F: Fn(&CowRow) -> i32 + 'static { ... }
明确声明
F
有生命周期'a
并且Box
(playpen):fn agg<'a, F>(col: F) -> Box<FnMut(&CowRow) -> i32 + 'a> where F: Fn(&CowRow) -> i32 + 'a { ... }
第二个版本比第一个版本更通用,并将接受更多的闭包作为参数。