3

I cloned https://gitlab.com/pwoolcoc/soup and ran the example file. Everything is ok, but when I create a new project with the same file as the example file, I encounter an error. the code is:

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 file

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

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

the error:

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;
    |
Matthieu M.
  • 287,565
  • 48
  • 449
  • 722
王奕然
  • 3,891
  • 6
  • 41
  • 62
  • 2
    The help message seems a little bit messed up. Also please read it, the solution is right there: `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: use soup::handle_ext::HandleExt;` – hellow Nov 29 '19 at 09:40

1 Answers1

6

The example is using reqwest-0.9 (check soup's manifest), which in turn is using the older blocking interface by default.

You are using reqwest-0.10, which uses the async interface by default. This is why you get a Future in reqwest::get(...), which in fact does not impl Try.

Either use reqwest-0.9 in your dependencies, use reqwest::blocking::get() or request::get(...).await? with all the async-bells and -whistles.

user2722968
  • 13,636
  • 2
  • 46
  • 67