为什么 get 方法没有在 reqwest 中返回 Response 对象?

问题描述

我正在尝试从 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 `()`

我对这种行为感到有点惊讶,因为我的代码几乎是ipsis litteris文档代码。

我认为 ? 仅适用于 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 对象,就像文档一样?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)