1
0
mirror of https://github.com/google/comprehensive-rust.git synced 2025-07-06 22:45:40 +02:00

Simplify join_all example

This keeps the original structure, but removes 7 lines of code.
This commit is contained in:
Martin Geisler
2024-06-20 00:29:42 +02:00
parent 512878a3e9
commit 8d8a8b6f76

View File

@ -1,24 +1,17 @@
use anyhow::Result; use futures::future::join_all;
use futures::future;
use reqwest; use reqwest;
use std::collections::HashMap;
async fn size_of_page(url: &str) -> Result<usize> { async fn size_of_page(url: &str) -> reqwest::Result<usize> {
let resp = reqwest::get(url).await?; let resp = reqwest::get(url).await?;
Ok(resp.text().await?.len()) Ok(resp.text().await?.len())
} }
#[tokio::main] #[tokio::main]
async fn main() { async fn main() {
let urls: [&str; 4] = [ let urls = ["https://rust-lang.org", "https://httpbin.org/ip", "BAD_URL"];
"https://google.com", let futures = urls.into_iter().map(size_of_page);
"https://httpbin.org/ip", let results = join_all(futures).await;
"https://play.rust-lang.org/", for (url, result) in urls.into_iter().zip(results) {
"BAD_URL", println!("{url}: {result:?}");
]; }
let futures_iter = urls.into_iter().map(size_of_page);
let results = future::join_all(futures_iter).await;
let page_sizes_dict: HashMap<&str, Result<usize>> =
urls.into_iter().zip(results.into_iter()).collect();
println!("{:?}", page_sizes_dict);
} }