gpt4 book ai didi

compiler-errors - 找到预期的 XYZ ()

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

例如:

use futures::future::Future;

fn main() {
let (stop_tokio, time_to_stop) = tokio::sync::oneshot::channel::<()>();
let handler = std::thread::spawn(|| {
tokio::run(
time_to_stop, // .map_err(|_| ())
);
});
handler.join().expect("join failed");
}

编译器打印错误:

error[E0271]: type mismatch resolving `<tokio_sync::oneshot::Receiver<()> as futures::future::Future>::Error == ()`
--> src/main.rs:6:9
|
6 | tokio::run(
| ^^^^^^^^^^ expected struct `tokio_sync::oneshot::error::RecvError`, found ()
|
= note: expected type `tokio_sync::oneshot::error::RecvError`
found type `()`
= note: required by `tokio::runtime::threadpool::run`

代码需要 (),却得到了 RecvError,但编译器打印出相反的结果。

这是编译器中的错误,还是我遗漏了什么?

最佳答案

从表面上看,tokio::run 期望 Future 具有关联的 Error 类型 ( ),但是 Receiveractual Future 实现关联了 Error 类型 RecvError.

然而,Rust 的类型推断是双向的,预期类型和实际类型有时可以反过来看。 通常消息的措辞符合您的期望,但也有像这种情况那样让人感觉倒退的情况。当然,即使没有以最好的方式报告,也不难弄清楚发生了什么并知道类型不匹配发生的位置。

在一般情况下,将人类对哪种类型是“实际” 以及哪种类型是“预期” 的解释编码可能不是一个容易解决的问题,但我同意此错误消息在您提供的代码中造成混淆。

我找不到与此相关的问题,但我确信我之前已经多次看到过这个话题。如果以前被举报过,再举报也没什么坏处,所以我就这么做了。

关于compiler-errors - 找到预期的 XYZ (),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55694827/

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