我正在学习 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 `()`
Run Code Online (Sandbox Code Playgroud)
代码如下。我已经多次浏览文档,但我返回了 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(());
}
Run Code Online (Sandbox Code Playgroud)
谢谢你!
Mas*_*inn 11
由于正在执行转换,该消息有点令人困惑,您可能想在错误跟踪器上报告这一点,但请注意措辞:
?^ 不能在返回的异步块中使用运算符()
它使用“块”这个词,而不是功能。因为这实际上是一个级联错误:在幕后,async函数被转换为状态机,其中每个状态机await都是代码块之间的“屈服点”,因此
let res = client
.post(path)
.body(body)
.send()
.await?;
let text = res.text().await?;
println!("{}", text);
Ok(());
Run Code Online (Sandbox Code Playgroud)
可以这样想(这不是实际的转换,如果你想要真正的解构,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;
Run Code Online (Sandbox Code Playgroud)
请注意最后一个块的最后一个表达式,这就是问题发生的地方:它是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 `()`
Run Code Online (Sandbox Code Playgroud)
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`
Run Code Online (Sandbox Code Playgroud)