无法在 `reqwest` 中找到 `blocking`。

56

我正在尝试使用reqwest 0.10.0-alpha.2从给定的URL下载文本文件,这看起来是一个合适的工具。我已经在我的Cargo.toml文件中添加了以下内容:

[package]
name = "..."
version = "0.1.0"
authors = ["Y*** <y***@***.***>"]
edition = "2019"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
reqwest = "0.10.0-alpha.2"

依赖似乎已经解决,我有了Cargo.lock文件。
我从文档中提取了这个片段。
let body = reqwest::blocking::get("https://www.rust-lang.org")?
    .text()?;

println!("body = {:?}", body);

但是我遇到了这个错误:

  |  
  |     let body = reqwest::blocking::get("https://www.rust-lang.org")?.text()?;  
  |                         ^^^^^^^^ could not find `blocking` in `reqwest`  
为什么?我在上面的链接中看到了这一行:“这需要启用可选的阻止功能”。可能只是这个原因。然而,对于我来说,在Rust中如何为库启用“功能”并不清楚。
我也尝试过这个(有些盲目尝试):
use reqwest::blocking;

同样的错误:

 |
 | use reqwest::blocking;
 |     ^^^^^^^^^^^^^^^^^ no `blocking` in the root

按照@edwardw的回答,启用"reqwest"中的"blocking",然后还需要将?更改为unwrap。不确定,但也许?来自rust的旧版本或其他东西。但对我来说它无法编译。

let body = reqwest::blocking::get("https://www.rust-lang.org")
    .unwrap()
    .text();
println!("body = {:?}", body);

1
似乎reqwest文档不太完善: https://www.gitmemory.com/issue/seanmonstar/reqwest/643/532880897 - num8er
1个回答

83

这是一个可选功能的模块。你需要在依赖项中显式启用它:

[dependencies]
reqwest = { version = "0.10.0-alpha.2", features = ["blocking"] }

reqwest::blocking文档提到了这一点:

这需要启用可选的blocking特性。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接