我想使用闭包的功能参数:
fn foo(f: Box<Fn() -> bool>) -> bool {
f()
}
fn main() {
let bar = 42;
foo(Box::new(|| bar != 42));
}
但我得到这个一生的错误:
src/main.rs:7:24: 7:36 error: cannot infer an appropriate lifetime due to conflicting requirements
src/main.rs:7 let n = foo(Box::new(|| bar != 42));
^~~~~~~~~~~~
src/main.rs:7:15: 7:23 note: first, the lifetime cannot outlive the expression at 7:14...
src/main.rs:7 let n = foo(Box::new(|| bar != 42));
^~~~~~~~
src/main.rs:7:15: 7:23 note: ...so that the type `[closure src/main.rs:7:24: 7:36]` will meet its required lifetime bounds
src/main.rs:7 let n = foo(Box::new(|| bar != 42));
^~~~~~~~
src/main.rs:7:15: 7:37 note: but, the lifetime must be valid for the call at 7:14...
src/main.rs:7 let n = foo(Box::new(|| bar != 42));
^~~~~~~~~~~~~~~~~~~~~~
src/main.rs:7:24: 7:36 note: ...so that argument is valid for the call
src/main.rs:7 let n = foo(Box::new(|| bar != 42));
^~~~~~~~~~~~
error: aborting due to previous error
我不明白为什么寿命不正确infered。 我能做些什么来解决这个问题?
$ rustc --version
rustc 1.0.0-nightly (6c065fc8c 2015-02-17) (built 2015-02-18)