以下代码无法编译.
fn main() {
let foo = bar(8);
println!("Trying `foo` with 4: {:d}", foo(4));
println!("Trying `foo` with 8: {:d}", foo(8));
println!("Trying `foo` with 13: {:d}", foo(13));
}
//
fn bar(x: int) -> (|int| -> int) {
|n: int| -> int {
if n < x { return n }
x
}
}
Run Code Online (Sandbox Code Playgroud)
错误如下.
11:32 error: explicit lifetime bound required
.../hello/src/main.rs:11 fn bar(x: int) -> (|int| -> int) {
^~~~~~~~~~~~
Run Code Online (Sandbox Code Playgroud)
我bar通过值传递整数参数.为什么Rust关心值传递的整数的生命周期?编写返回闭包的函数的正确方法是什么?谢谢.
编辑
我在手册中找到了以下内容. In the simplest and least-expensive form …
rust ×1