在“reqwest"中找不到“blocking" [英] could not find `blocking` in `reqwest`

查看:66
本文介绍了在“reqwest"中找不到“blocking"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 reqwest 0.10.0-alpha.2,它看起来像一个合适的工具.我的 Cargo.toml 文件中有这个:

[包]名称 = "..."版本 = "0.1.0"作者 = ["Y*** "]版本=2019"# 在 https://doc.rust-lang.org/cargo/reference/manifest.html 查看更多键及其定义[依赖项]reqwest = "0.10.0-alpha.2"

依赖似乎解决了,我有我的 Cargo.lock 文件.

我从文档

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

但我收到此错误:

<块引用>

 ||let body = reqwest::blocking::get("https://www.rust-lang.org")?.text()?;|^^^^^^^^ 在`reqwest` 中找不到`blocking`

为什么?我确实在上面的链接中的文档这需要启用可选的阻止功能"上看到这一行.可能只是这样.但是,我也不清楚如何在 Rust 中为库启用功能".

<小时>

我也试过这个(一些在黑暗中拍摄):

使用 reqwest::blocking;

同样的错误:

<块引用>

 ||使用 reqwest::blocking;|^^^^^^^^^^^^^^^^^ 根中没有`阻塞`

<小时>

按照@edwardw 的回答在reqwest"中启用阻塞",然后还必须将 ? 更改为 unwrap.不确定,但也许 ? 来自旧版本的 rust 或 sth.但它不为我编译.

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

解决方案

这是 crate 的可选功能.您必须在依赖项中显式启用它:

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

reqwest::blocking 文档 确实提到了它:

<块引用>

这需要启用可选的blocking 功能.

I am trying to download a text file from a given URL using reqwest 0.10.0-alpha.2, which looks like an appropriate tool. I have this in my Cargo.toml file:

[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"

The dependency seems to resolve and I have my Cargo.lock file.

I have this snippet lifted from the docs

let body = reqwest::blocking::get("https://www.rust-lang.org")?
    .text()?;

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

But I am getting this error:

  |  
  |     let body = reqwest::blocking::get("https://www.rust-lang.org")?.text()?;  
  |                         ^^^^^^^^ could not find `blocking` in `reqwest`  

Why? I do see this line on the doc "This requires the optional blocking feature to be enabled" from the above link. It might be just that. However, it is not clear to me how you enable a "feature" for a library in Rust either.


I also tried this (some shooting in the dark):

use reqwest::blocking;

Same error:

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


Following @edwardw's answer to enable "blocking" in "reqwest", and then also have to change ? to unwrap. Not sure, but maybe ? is from an older version of rust or sth. But it doesn't compile for me.

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

解决方案

It is an optional feature of the crate. You have to explicitly enable it in dependencies:

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

The reqwest::blocking documentation does mention it:

This requires the optional blocking feature to be enabled.

这篇关于在“reqwest"中找不到“blocking"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆