如果我有一个返回函数的函数:
fn<'r, T> ( p : T ) -> (&'r fn(&'r str) -> ~[(T,int)]) {
return |s| ~[(p, 0)]
}
但是,这似乎不起作用,我收到以下(有点重言式)错误:
playground.rs:10:8: 10:29 error: cannot infer an appropriate lifetime due to conflicting requirements
playground.rs:10 return |s| ~[(p, 0i)]
^~~~~~~~~~~~~~~~~~~~~
playground.rs:9:70: 11:5 note: first, the lifetime cannot outlive the block at 9:70...
playground.rs:9 pub fn result<'r, T>( p : T ) -> (&'r fn(&'r str) -> ~[(T, int)] ){
playground.rs:10 return |s| ~[(p, 0i)]
playground.rs:11 }
playground.rs:10:8: 10:29 note: ...due to the following expression
playground.rs:10 return |s| ~[(p, 0i)]
^~~~~~~~~~~~~~~~~~~~~
playground.rs:9:70: 11:5 note: but, the lifetime must be valid for the lifetime &'r as defined on the block at 9:70...
playground.rs:9 pub fn result<'r, T>( p : T ) -> (&'r fn(&'r str) -> ~[(T, int)] ){
playground.rs:10 return |s| ~[(p, 0i)]
playground.rs:11 }
playground.rs:10:8: 10:29 note: ...due to the following expression
playground.rs:10 return |s| ~[(p, 0i)]
^~~~~~~~~~~~~~~~~~~~~
error: aborting due to previous error
我相信这是说函数签名的返回的生命周期和返回值不匹配。但是,我不确定如何用生命周期注释 lambda 以使其工作。