rust - `?` 运算符只能在返回 `Result` 或 `Option 的异步块中使用
问题描述
我正在学习 rust 并试图通过发送一些 POST 数据来抓取一个随机站点,但我遇到了一堆错误,例如:
error[E0277]: the `?` operator can only be used in an async block that returns `Result` or `Option` (or another type that implements `FromResidual`)
37 | | let text = res.text().await?;
| | ^ cannot use the `?` operator in an async block that returns `()`
代码如下。我已经多次浏览文档,但我返回一个 Result<> 所以我不确定出了什么问题。
use std::sync::Arc;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
println!("Hello, world!");
use reqwest::{cookie::Jar, Url};
use reqwest::header;
let mut headers = header::HeaderMap::new();
headers.insert("User-Agent", header::HeaderValue::from_static("Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/94.0.4606.81 Safari/537.36"));
headers.insert("Content-Type", header::HeaderValue::from_static("application/json"));
let cookie = "cookies";
let jar = Jar::default();
let cookie_url = "https://url.com/".parse::<Url>()?;
jar.add_cookie_str(cookie, &cookie_url);
let body = String::from("post body");
let client = reqwest::Client::builder()
.default_headers(headers)
.cookie_store(true)
.cookie_provider(Arc::new(jar))
.build()?;
let path = "https://long_url.com".parse::<Url>()?;
let res = client
.post(path)
.body(body)
.send()
.await?;
let text = res.text().await?;
println!("{}", text);
Ok(());
}
谢谢!
解决方案
由于正在执行的转换,该消息有点令人困惑,您可能希望在错误跟踪器上报告这一点,但请注意措辞:
^ 不能在返回的异步块
?
中使用运算符()
它使用“块”这个词,而不是功能。因为这实际上是一个级联错误:在幕后,一个async
函数被转换为一个状态机,其中每个await
都是代码块之间的“屈服点”,所以
let res = client
.post(path)
.body(body)
.send()
.await?;
let text = res.text().await?;
println!("{}", text);
Ok(());
可以按照以下思路来考虑(这不是实际的转换,如果您想要实际的解构,Jon Gjengset 有关于该主题的大量视频)
let future = async {
client.post(path).body(body).send()
};
yield future;
let future = async {
let res = future.value()?;
res.text()
};
yield future;
let future = async {
let text = future.value()?;
println!("{}", text);
Ok(());
};
return future;
注意最后一个块的最后一个表达式,这就是问题发生的地方:它是Ok(());
.
尾随分号将“抑制”表达式的正常值并导致 a ()
, not Result<...>
,因此类型不兼容和您看到的错误:块的返回值不连贯。
在一堆异步块错误的末尾,编译器实际上显示了“源”错误:
error[E0308]: mismatched types
--> src/main.rs:6:20
|
6 | async fn main() -> Result<(), Box<dyn std::error::Error>> {
| ---- ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected enum `Result`, found `()`
| |
| implicitly returns `()` as its body has no tail or `return` expression
|
= note: expected enum `Result<(), Box<(dyn std::error::Error + 'static)>>`
found unit type `()`
如果您查看整个“异步块”错误,实际上每次都会注意显示Ok(());
错误:
error[E0277]: the `?` operator can only be used in an async block that returns `Result` or `Option` (or another type that implements `FromResidual`)
--> src/lib.rs:19:55
|
6 | async fn main() -> Result<(), Box<dyn std::error::Error>> {
| ___________________________________________________________-
7 | | println!("Hello, world!");
8 | |
9 | | use reqwest::{cookie::Jar, Url};
... |
19 | | let cookie_url = "https://url.com/".parse::<Url>()?;
| | ^ cannot use the `?` operator in an async block that returns `()`
... |
41 | | Ok(());
42 | | }
| |_- this function should return `Result` or `Option` to accept `?`
|
= help: the trait `FromResidual<Result<Infallible, url::parser::ParseError>>` is not implemented for `()`
note: required by `from_residual`
推荐阅读
- neo4j - Neo4J Enterprise 比 Community 慢?
- encoding - 图像的编码系统?
- tomcat - 运行 Curl 命令工具时需要帮助隐藏服务器特定错误
- pandas - 用其他列多次替换一列中的值
- javascript - jquery.inputmask.js 不返回未屏蔽的值
- python - Docker容器无法重启,因为python应用服务被吸,无法被杀死
- python - 如何在 Python 中从文件中读取整数
- java - 使用netbeans和H2服务器的spring web mvc项目中的sessionFactory问题
- polling - 如何在 Svelte 中进行轮询?
- java - 实现的安全依赖和登录页面