closures - 如何从函数返回 Rust 闭包?

标签 closures rust

尝试学习 Rust,似乎我很难找到如何返回 0.13 的函数(每晚)。我的基本示例是尝试处理不可变的参数,因此我希望以下内容能够工作。当我在线阅读时,似乎在 0.13 中行为会发生变化(所以我在线阅读的所有内容似乎都不起作用)。

$ rustc --version
rustc 0.13.0-nightly (62fb41c32 2014-12-23 02:41:48 +0000)

刚起床 http://doc.rust-lang.org/0.12.0/guide.html#accepting-closures-as-arguments下一个逻辑步骤是返回一个闭包。当我这样做时,编译器说我不能

#[test]
fn test_fn_return_closure() {
  fn sum(x: int) -> |int| -> int {
    |y| { x + y }
  }

  let add3 = sum(3i);
  let result: int = add3(5i);

  assert!(result == 8i);
}

/rust-lang-intro/closures/tests/lib.rs:98:21: 98:33 error: explicit lifetime bound required
/rust-lang-intro/closures/tests/lib.rs:98   fn sum(x: int) -> |int| -> int {
                                                                                                   ^~~~~~~~~~~~
error: aborting due to previous error
Could not compile `closures`.

我尝试返回一个引用,看看是否有帮助

let sum = |x: int| {
  &|y: int| { x + y }
};
let add3 = *(sum(3i));

但是当您尝试使用它时,您会收到更详细的错误

/rust-lang-intro/closures/tests/lib.rs:102:6: 102:24 error: cannot infer an appropriate lifetime for borrow expression due to conflicting requirements
/rust-lang-intro/closures/tests/lib.rs:102     &|y: int| { x + y }
                                                ^~~~~~~~~~~~~~~~~~
/rust-lang-intro/closures/tests/lib.rs:105:14: 105:25 note: first, the lifetime cannot outlive the expression at 105:13...
/rust-lang-intro/closures/tests/lib.rs:105   let add3 = *(sum(3i));
                                                        ^~~~~~~~~~~
/rust-lang-intro/closures/tests/lib.rs:105:14: 105:25 note: ...so that pointer is not dereferenced outside its lifetime
/rust-lang-intro/closures/tests/lib.rs:105   let add3 = *(sum(3i));
                                                        ^~~~~~~~~~~
/rust-lang-intro/closures/tests/lib.rs:102:6: 102:24 note: but, the lifetime must be valid for the expression at 102:5...
/rust-lang-intro/closures/tests/lib.rs:102     &|y: int| { x + y }
                                                ^~~~~~~~~~~~~~~~~~
/rust-lang-intro/closures/tests/lib.rs:102:6: 102:24 note: ...so type `|int| -> int` of expression is valid during the expression
/rust-lang-intro/closures/tests/lib.rs:102     &|y: int| { x + y }
                                                                                     ^~~~~~~~~~~~~~~~~~
error: aborting due to previous error
Could not compile `closures`.

所以,我假设我需要保存指针,并且仅在需要时取消引用,但似乎错误消息基本相同。

最佳答案

在当前的 1.1.0 时代 Rust 中,这是有详细记录的。

#[cfg(test)]
mod tests {
    // the `type` is not needed but it makes it easier if you will
    // be using it in other function declarations.
    type Adder = Fn(i32) -> i32;

    fn sum(x: i32) -> Box<Adder> {
        Box::new(move |n: i32| x + n)
    }

    #[test]
    fn it_works() {
        let foo = sum(2);
        assert_eq!(5, foo(3));
    }
}

参见the Rust docs了解更多详情。

关于closures - 如何从函数返回 Rust 闭包?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27683144/

相关文章:

javascript - 为什么我不能在封闭函数之外重构这个匿名函数?

python - 在 Ruby 中可以像这样使用 Lambdas 吗?

rust - 二维旋转

inheritance - Rust 中的方法覆盖替代

rust - 为什么无法使用方括号 [ ] 表示法更新 HashMap 中的值?

closures - 函数闭包与延续,一般和 SML

javascript - 如何在作用域内创建全局函数

php - 为什么 C++11 和 PHP 闭包需要声明闭包变量?

performance - 对同步的性能影响感到困惑

arrays - 在 Rust 中如何在编译时确定数组的大小?