【发布时间】: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