How can I define an async method in a trait?

On stable Rust, async fn cannot be used in traits. There is ongoing work that will make this possible in the future, but the easiest solution currently is to use the async-trait crate: #[async_trait] trait Readable { async fn readable(&self) -> io::Result<()>; } #[async_trait] impl Readable for Reader { async fn readable(&self) -> io::Result<()> { … Read more

How can I perform parallel asynchronous HTTP GET requests with reqwest?

Concurrent requests As of reqwest 0.10: use futures::{stream, StreamExt}; // 0.3.5 use reqwest::Client; // 0.10.6 use tokio; // 0.2.21, features = [“macros”] const CONCURRENT_REQUESTS: usize = 2; #[tokio::main] async fn main() { let client = Client::new(); let urls = vec![“https://api.ipify.org”; 2]; let bodies = stream::iter(urls) .map(|url| { let client = &client; async move { let … Read more