Skip to content

Instantly share code, notes, and snippets.

@rust-play
Created July 11, 2019 19:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rust-play/701006c875042230721c8371a4e25f0f to your computer and use it in GitHub Desktop.
Save rust-play/701006c875042230721c8371a4e25f0f to your computer and use it in GitHub Desktop.
Code shared from the Rust Playground
/*
reqwest = "0.9.18"
tokio = "*"
futures = "*"
*/
use futures::{Future, Stream, future};
use reqwest::r#async::Client;
use tokio::runtime::current_thread::Runtime;
fn fetch(url: &str) -> impl Future<Item=&str, Error=()> {
Client::new()
.get(url)
.send()
.and_then(|res| {
res.into_body()
.concat2()
})
.and_then(move |buf| {
future::ok(::std::str::from_utf8(&buf).unwrap())
})
.map_err(|err| panic!("request error: {}", err))
}
fn main() {
let s = Runtime::new().unwrap().block_on(fetch("http://www.example.com/"));
println!("{:?}", s);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment