rust - 特征 std::ops::Try 没有为 impl 实现

标签 rust

我克隆了 https://gitlab.com/pwoolcoc/soup并运行示例文件。一切正常,但是当我使用与示例文件相同的文件创建新项目时,我遇到了错误。 代码是:

use std::collections::HashSet;
use std::fmt::Error;
use soup::Soup;
use reqwest;
fn main() -> Result<(), Box<Error>> {
    let resp = reqwest::get("https://docs.rs/soup/0.1.0/soup/")?;
    let soup = Soup::from_reader(resp)?;
    let result = soup
        .tag("section")
        .attr("id", "main")
        .find()
        .and_then(|section| {
            section
                .tag("span")
                .attr("class", "in-band")
                .find()
                .map(|span| span.text())
        });
    assert_eq!(result, Some("Crate soup".to_string()));
    Ok(())
}

cargo 文件

[package]
name = "testquestion"
version = "0.1.0"
authors = ["wangyiran"]
edition = "2018"

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

错误:

error[E0277]: the `?` operator can only be applied to values that implement `std::ops::Try`
   --> src\main.rs:6:16
   | 6 |     let resp = reqwest::get("https://docs.rs/soup/0.1.0/soup/")?;
   |                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the `?` operator cannot be applied to type `impl std::future::Future`
   |   = help: the trait `std::ops::Try` is not implemented for `impl std::future::Future`
       = note: required by `std::ops::Try::into_result`

error[E0277]: `?` couldn't convert the error to `std::boxed::Box<std::fmt::Error>`
   --> src\main.rs:7:39
   | 7 |    let soup = Soup::from_reader(resp)?;
   |                                          ^ the trait `std::convert::From<std::io::Error>` is not implemented for `std::boxed::Box<std::fmt::Error>`
   |   = note: the question mark operation (`?`) implicitly performs a conversion on the error value using the `From` trait
       = help: the following implementations were found:
            <std::boxed::Box<(dyn std::error::Error + 'a)> as std::convert::From<E>>
            <std::boxed::Box<(dyn std::error::Error + 'static)> as std::convert::From<&str>>
            <std::boxed::Box<(dyn std::error::Error + 'static)> as std::convert::From<std::borrow::Cow<'a, str>>>
            <std::boxed::Box<(dyn std::error::Error + 'static)> as std::convert::From<std::string::String>>
          and 16 others   = note: required by `std::convert::From::from`

error[E0599]: no method named `tag` found for type `std::rc::Rc<markup5ever::rcdom::Node>` in the current scope
   --> src\main.rs:14:18
    | 14 |                 .tag("span")
    |                       ^^^ method not found in `std::rc::Rc<markup5ever::rcdom::Node>`
    |   = help: items from traits can only be used if the trait is in scope 
        = help: the following trait is implemented but not in scope, perhaps add a `use` for it:
    | 1  | use soup::handle_ext::HandleExt;
    |

最佳答案

该示例使用的是 reqwest-0.9(检查 soup 的 list ),它又默认使用旧的阻塞接口(interface)。

您正在使用 reqwest-0.10,它默认使用异步接口(interface)。这就是为什么你在 reqwest::get(...) 中得到一个 Future,实际上它并不意味着 Try

在您的依赖项中使用 reqwest-0.9,使用 reqwest::blocking::get()request::get(...)。 await? 所有异步铃声和口哨声。

关于rust - 特征 std::ops::Try 没有为 impl 实现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59101974/

相关文章:

rust - 是否可以为由所有实现特征的类型组成的任何元组自动实现特征?

reference - 在 RUST 中借用和捕获变量

multithreading - 如何在不使用跟踪句柄的情况下等待未知数量的 Rust 线​​程完成?

glsl - 使用 Glium 中的 UniformBuffer 将任意大小的对象传递给片段着色器

function - 有没有办法避免取消对特征对象的引用?

serialization - 我们如何编写一个通用函数来检查 Serde 序列化和反序列化?

rust - 这个 for 循环模式有没有名字,如果有,有没有更好的写法?

macos - 如何在 Nix 环境中使 macOS 框架可用于 clang?

Rust 使用 Reqwest 处理错误响应体

rust - 如何在字符串字符的反向迭代器中从头开始获取索引?