相关疑难解决方法(0)

Rust函数返回一个闭包:``需要显式生命周期绑定"

以下代码无法编译.

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

4
推荐指数
2
解决办法
2381
查看次数

标签 统计

rust ×1