- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试从reqwest documentation复制一个示例。这是示例:
let body = reqwest::get("https://www.rust-lang.org")?
.text()?;
在文件Cargo.toml中添加
reqwest = "0.10.10"
行之后,在我的
main.rs
文件中添加以下代码:
extern crate reqwest;
fn main() {
let body = reqwest::get("https://www.rust-lang.org")?.text()?;
println!("body = {:?}", body);
}
此代码不会编译并返回以下错误:
cannot use the `?` operator in a function that returns `()`
我对这种行为感到有些惊讶,因为我的代码几乎是文档代码。
?
仅适用于Response对象,因此我检查了
get
方法返回的对象:
extern crate reqwest;
fn print_type_of<T>(_: &T) {
println!("{}", std::any::type_name::<T>())
}
fn main() {
let body = reqwest::get("https://www.rust-lang.org");
print_type_of(&body);
}
输出:
core::future::from_generator::GenFuture<reqwest::get<&str>::{{closure}}
我的意思是,为什么不像文档一样没有得到Response对象?
最佳答案
这里有两个单独的问题使您绊倒。
您链接到的文档是针对reqwest
版本0.9.18
的,但是您已经安装了0.10.10
版本。如果查看the docs for 0.10.10
,将会看到您使用的代码段是
let body = reqwest::get("https://www.rust-lang.org")
.await?
.text()
.await?;
println!("body = {:?}", body);
或更可能的情况是,因为您没有提到具有异步运行时,所以
the blocking
docs
let body = reqwest::blocking::get("https://www.rust-lang.org")?
.text()?;
println!("body = {:?}", body);
请注意,当您尝试使用此功能时,您仍会得到
cannot use the
?
operator in a function that returns()
main
上设置返回类型,例如
fn main() -> Result<(), reqwest::Error> {
有关更多信息,请参见
Why do try!() and ? not compile when used in a function that doesn't return Option or Result?。
关于rust - 为什么get方法未在reqwest中返回Response对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65556721/
我正在尝试编写一个与 Todoist 的 REST API 通信的库。这个想法是库公开了一个 TodoistAPI 结构,其中包含一个 reqwest::Client 和一个 base_url。有一个
我是rust的新手,正在尝试获取请求响应主体,我当前的代码是: let client = reqwest::Client::new(); let response_text = client.get(
我正在尝试运行基本的 reqwest example : extern crate reqwest; extern crate tokio; #[tokio::main] async fn main(
我正在尝试在我正在编写的Rust程序中使用this Python script(取自here)的某些部分。如何构造具有相同内容的reqwest请求? def login(login_url, user
我正在使用 reqwest (版本 0.10.4)用于 Rust 应用程序中的 HTTP 调用,但找不到任何示例说明如何处理可能返回多个可能响应主体的 API 调用,主要用于错误处理。 例如,API
我发现的只是 ClientBuilder 有一个选项可以设置每台主机的最大空闲连接数,但这似乎并没有对可用连接数提供总体限制。 最佳答案 看起来 Reqwest 工作在 Tokio 之上。您可以 co
我很困惑为什么我没有从以下使用 reqwest 的函数中获取任何内容: fn try_get() { let wc = reqwest::Client::new(); wc
当我运行以下代码时: use exitfailure::ExitFailure; use reqwest::Url; use serde_derive::{Deserialize, Serialize
我对 Rust 很陌生,我似乎无法找到解决这个问题的方法。我正在尝试以 json 形式获取 get 请求的响应。 #[macro_use] extern crate serde; extern cra
通过 reqwest 的请求需要长轮询响应,因此我创建了一个字节流并尝试将每个块反序列化为 JSON。我立即意识到这是错误的,因为每个块都可能不完整,因此反序列化可能会失败;尽管一些块被反序列化为 J
我一直在研究Rust,并遇到了以下代码: fn request(&url) -> Result> { let mut res = reqwest::get(&url)?; let mut bo
我有一个包含以下日期的字符串 2015-07-05T11:02:25+00:00。 我使用下面的代码来解析它(使用 chrono crate) let pub_date = str::repl
我正在尝试使用 Reqwest 的代理功能将 user:pass basic auth 与 URL 的其余部分一起传递到代理功能中。显然,这个 crate 的基本身份验证工作方式不能以这种方式传递给代
我试图让一个基本的 reqwest http-request 像这样进行 extern crate reqwest; extern crate url; use url::Url; fn main()
我需要下载一个 60MB 的 ZIP 文件并提取其中的唯一文件。我想下载它并使用流提取它。我如何使用 Rust 实现这一点? fn main () { let mut res = reqwes
这个问题在这里已经有了答案: How do you enable a Rust "crate feature"? (1 个回答) 关闭 3 年前。 我正在尝试使用 reqwest 0.10.0-al
我需要使用 Reqwest 库向带有 cookie 的网站发出 GET 请求。我想出了如何发送 GET 请求: let response = reqwest::get("http://example.
在过去的几天里,我一直在搞乱 HTTP,在 Rust 中构建了一个简单的 CL 下载管理器,使用 reqwest crate 来处理 HTTP 内容。我对协议(protocol)的工作原理有了基本的了
我正在使用以下系统来处理 AJAX 上传:https://github.com/ded/reqwest 虽然它适用于我迄今为止尝试过的所有内容 - 我现在需要上传文件(当输入更改时)。你怎样做呢?到目
在我的 actix-web 服务器中,我尝试使用 reqwest 调用外部服务器,然后将响应返回给用户。 use actix_web::{web, App, HttpResponse, HttpSer
我是一名优秀的程序员,十分优秀!