Reqwest: Examples with `?` operator not working?

Created on 28 Mar 2018  路  9Comments  路  Source: seanmonstar/reqwest

Hi there, I'm new to Rust so this might not be related to reqwest. As far as I understand the ? operator can only be used inside functions that return a Result. However in the examples this isn't mentioned. Simply running the first example:

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

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

Returns cannot use the ? operator in a function that returns () and body not found in this scope. Then changing the code to:

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

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

Returns no method named text found for type std::result::Result<reqwest::Response, reqwest::Error>, any help appreciated.

easy

Most helpful comment

None of the above examples work on Rust 1.40.......

same here

All 9 comments

try:

extern crate reqwest;

fn foo() -> Result<(), reqwest::Error> {
    let text = reqwest::get("https://www.rust-lang.org")?
        .text()?;

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

fn main() {
    foo().unwrap();
}

Thanks @messense that works & confirms my guess that it only works within a function returning a Result. Do you think it's a good idea to simplify the example so it works anywhere in main? For me it caused some confusion.

The examples were changed to show using ? because the community as a whole wanted to move away from showing examples where errors just cause crashes (by unwraping all errors). Eventually (seems like about a couple months), it'll be possible to return an error from the main function, and so examples can be update to that!

In the meantime, this might be helpful: have the very first example show using in fn main and an auxiliary fn run, simply calling run().unwrap() in main. Then, have a note after the example that all other examples assume a similar setup. What do you think of that?

I dug into this a bit and indeed found it is the recommended way to write examples. I'm fond of copy and pasting snippets to try but I can see how this provides a cleaner way of showing functionality. Guess most people won't get as confused as I was about the ? unless they're completely new to Rust like me.

Will create a PR!

seems that even without a function is now supported.

extern crate reqwest;

fn main() -> reqwest::Result<()> {
    let body = reqwest::get("https://www.rust-lang.org")?.text()?;

    println!("{}", body);

    Ok(())
}

tested on rustc 1.33.0 (2aa4c46cf 2019-02-28)

None of the above examples work on Rust 1.40.......

None of the above examples work on Rust 1.40.......

same here

Confirmed. It does not work on 1.4.3 either.

same problem here.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

29e7e280-0d1c-4bba-98fe-f7cd3ca7500a picture 29e7e280-0d1c-4bba-98fe-f7cd3ca7500a  路  6Comments

theduke picture theduke  路  6Comments

dbrgn picture dbrgn  路  5Comments

CGQAQ picture CGQAQ  路  4Comments

jeprojects picture jeprojects  路  3Comments