【问题标题】:"Closure takes 0 arguments" on a closure that takes an argument带有参数的闭包上的“闭包接受 0 个参数”
【发布时间】:2018-01-26 03:54:28
【问题描述】:

我试图在条件之后将闭包传递给函数:

extern crate futures;
extern crate tokio_core;

use futures::prelude::*;
use futures::future;
use futures::unsync::*;

use tokio_core::reactor::*;

fn main() {
    let mut core = Core::new().unwrap();
    let h2 = core.handle();

    let fun = |should: Result<bool, ()>| {
        if should.unwrap_or(true) {
            // do something
        }

        future::ok(())
    };

    let (tx, rx) = oneshot::channel::<bool>();

    let dummy = true;

    if dummy {
        h2.spawn(rx.then(fun));
    } else {
        h2.spawn(future::ok(true).then(fun));
    }

    core.run(future::ok::<(), ()>(())).unwrap();
}

这里是the code in the playground,但它得到的错误与令我困惑的错误不同。我的错误看起来像:

error[E0593]: closure takes 0 arguments but 1 argument is required
   --> src/client/forger.rs:123:25
    |
57  |           let fun = |should: Result<bool, ()>| {
    |  ___________________-
58  | |             if !should.unwrap_or(false) {
59  | |                 return Box::new(future::ok::<(), ()>(()));
60  | |             }
...   |
117 | |             Box::new(future::ok(()))
118 | |         };
    | |_________- takes 0 arguments
...
123 |               h2.spawn(rx.then(fun));
    |                           ^^^^ expected closure that takes 1 argument

为什么 Rust 说函数 fun 接受 0 个参数,而它似乎引用它为接受一个参数?

我的 Rust 版本是 1.22.1。

【问题讨论】:

    标签: rust


    【解决方案1】:

    原来我的 Rust 版本是 1.22.1,但最新版本是 1.23.0。这就解释了为什么操场会返回一个更准确的错误:expected signature of fn(std::result::Result&lt;bool, futures::Canceled&gt;) -&gt; _... 这正是问题所在。

    【讨论】:

      猜你喜欢
      • 2022-07-27
      • 2013-05-26
      • 2021-02-07
      • 2012-09-16
      • 2021-01-07
      • 1970-01-01
      • 2011-08-06
      • 1970-01-01
      • 2022-01-14
      相关资源
      最近更新 更多