Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Examples with ? operator not working? #275

Closed
Siilwyn opened this issue Mar 28, 2018 · 9 comments
Closed

Examples with ? operator not working? #275

Siilwyn opened this issue Mar 28, 2018 · 9 comments
Labels
easy Easy! Start here :D

Comments

@Siilwyn
Copy link
Contributor

Siilwyn commented Mar 28, 2018

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.

@messense
Copy link
Contributor

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();
}

@Siilwyn
Copy link
Contributor Author

Siilwyn commented Mar 28, 2018

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.

@seanmonstar
Copy link
Owner

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?

@seanmonstar seanmonstar added the easy Easy! Start here :D label Mar 28, 2018
@Siilwyn
Copy link
Contributor Author

Siilwyn commented Apr 3, 2018

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!

seanmonstar pushed a commit that referenced this issue Apr 3, 2018
Clarify code example structure in combination with the `?` operator. Add
as separate section so it can be easily removed when `?` can be used
inside of `main`.

Closes #275
@sassman
Copy link

sassman commented Mar 11, 2019

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)

@cetanu
Copy link

cetanu commented Feb 8, 2020

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

@Vaxure1337
Copy link

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

same here

@MichaelARoberts
Copy link

Confirmed. It does not work on 1.4.3 either.

@palahacker
Copy link

same problem here.

Repository owner locked as resolved and limited conversation to collaborators Mar 11, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
easy Easy! Start here :D
Projects
None yet
Development

No branches or pull requests

8 participants