From 25247c72cc77e5ed8c6f8fa358ce5fcce2245582 Mon Sep 17 00:00:00 2001 From: Hatter Jiang Date: Thu, 30 Jan 2020 21:12:07 +0800 Subject: [PATCH] add async/.await --- async_await/Cargo.toml | 12 ++++++++++++ async_await/src/main.rs | 24 ++++++++++++++++++++++++ 2 files changed, 36 insertions(+) create mode 100644 async_await/Cargo.toml create mode 100644 async_await/src/main.rs diff --git a/async_await/Cargo.toml b/async_await/Cargo.toml new file mode 100644 index 0000000..1ed2a41 --- /dev/null +++ b/async_await/Cargo.toml @@ -0,0 +1,12 @@ +[package] +name = "async_await" +version = "0.1.0" +authors = ["Hatter Jiang "] +edition = "2018" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +futures = "0.3.1" +isahc = "0.8.2" +http = "0.1.21" diff --git a/async_await/src/main.rs b/async_await/src/main.rs new file mode 100644 index 0000000..caf03db --- /dev/null +++ b/async_await/src/main.rs @@ -0,0 +1,24 @@ + + +fn main() { + futures::executor::block_on(get_all()); +} + +async fn get_all() { + let mut v = vec![]; + for _ in 0..10 { + v.push(get_url("https://www.baidu.com/")); + } + + futures::future::join_all(v).await; +} + +async fn get_url(url: &str) { + println!("Start get: {}", url); + let r = isahc::get_async(url).await; + + match r { + Err(e) => println!("Get response error: {}", e), + Ok(o) => println!("Response: {:?}", o.status()), + }; +} \ No newline at end of file