在闭包Rust中使用函数指针参数

jos*_*osh 2 closures function-pointers lifetime rust

我期待创建一个函数,agg它接受另一个函数作为参数get_id,并返回一个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);
Run Code Online (Sandbox Code Playgroud)

哪会产生错误:

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
Run Code Online (Sandbox Code Playgroud)

这里的想法是我想要一个泛型函数,它允许创建在结构中不同字段上运行的闭包.所以,我的想法是传递一个函数,它是结构的getter,并在闭包中使用它来提取适当的字段.

我已经尝试了添加'staticagg签名的各种组合,但我不确定这实际意味着什么以及它需要在语法上去哪里.另外,我尝试了以下的一些技术:https://github.com/nrc/r4cppp/blob/master/closures.md,例如将get_id方法添加为特征,但也无法使其工作.

Djz*_*zin 5

F函数的类型参数具有关联的生命周期(就像所有其他类型一样).但是隐含地,你的函数的返回值Box<FnMut(&CowRow) -> i32>确实如此Box<FnMut(&CowRow) -> i32 + 'static>.也就是说,除非您为框指定生命周期,否则它会假定其内容可以永久存在.当然如果F只是为了生活'a,借用检查员会抱怨.要解决这个问题

第二个版本比第一个版本更通用,并且将接受更多闭包作为参数.