gpt4 book ai didi

rust - 带有参数的闭包上的 "Closure takes 0 arguments"

转载 作者:行者123 更新时间:2023-11-29 08:35:39 28 4
gpt4 key购买 nike

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

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.22.1,但最新版本是 1.23.0。这解释了为什么 Playground 返回更准确的错误:expected signature of fn(std::result::Result<bool, futures::Canceled>) -> _ ...这正是问题所在。

关于rust - 带有参数的闭包上的 "Closure takes 0 arguments",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48455581/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com