Downlading file from a website

I writed a simple program downloads file from a website but it gived a error saying

no method named `expect` found for opaque type `impl Future<Output = Result<Response, reqwest::Error>>` in the current scope
method not found in `impl Future<Output = Result<Response, reqwest::Error>>`
extern crate reqwest;

use std::io;
use std::fs::File;

fn main() {
    // create a new request
    let mut res = reqwest::get("").expect("failed to get url");
    
    // create a new file
    let mut file = File::create("").unwrap();
    
    // read the response into the file
    io::copy(&mut res, &mut file).unwrap();
}

reqwest is designed to be used with an asynchronous runtime, since requests to web servers can take a while so you often want to do something else while waiting for them. Accordingly, reqwest::get() is an async function, which means it actually returns a Future. To get the result of the request, you would need to wait for the Future to complete, which is usually handled by an asynchronous runtime and awaiting the Future.

For a simple program like this without an async runtime, you probably want to call reqwest::blocking::get(), which isn't async and will just wait for the request to return (blocking the thread while it does so).

1 Like

Ok thanks for the help :slight_smile:

For a simple program that doesn't need to run many concurrent transfers, a simpler library may be more appropriate than reqwest:

3 Likes

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.