Hello,
This is the first time I am trying to use async with Rust, however I get some weird error:
error[E0515]: cannot return value referencing local variable `repo`
--> src/main.rs:35:34
|
35 | .for_each(|(repo, resp)| async {
| __________________________________^
36 | | pb.inc(1);
37 | | match resp {
38 | | Ok(r) => match r.status() {
39 | | StatusCode::OK => println!("{}", repo),
| | ---- `repo` is borrowed here
... |
44 | | }
45 | | })
| |_________^ returns a value referencing data owned by the current function
Ideally, I'd like to collect all those repo
strings into the one Vec
and print it at the end of the program instead of doing println!
inside the async code. But before I was able to try constructing a vector, I got stuck with aforementioned error.
My code is here:
use futures::{stream, StreamExt};
use indicatif::ProgressBar;
use reqwest::{Client, StatusCode};
use std::collections::HashMap;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let client = Client::new();
let owner_alias = client
.get("https://src.fedoraproject.org/extras/pagure_owner_alias.json")
.send()
.await?
.json::<HashMap<String, HashMap<String, Vec<String>>>>()
.await?;
let keys = owner_alias["rpms"].keys();
let pb = ProgressBar::new(keys.len() as u64);
let dead_status = stream::iter(keys)
.map(|repo| {
let client = &client;
let url = format!(
"https://src.fedoraproject.org/rpms/{}/raw/master/f/dead.package",
repo
);
async move {
let resp = client.head(&url).send().await;
(repo, resp)
}
})
.buffer_unordered(16); // 16 requests in parallel
dead_status
.for_each(|(repo, resp)| async {
pb.inc(1);
match resp {
Ok(r) => match r.status() {
StatusCode::OK => println!("{}", repo),
StatusCode::NOT_FOUND => (),
_ => eprintln!("Unknown response: {:?}", r.status()),
},
Err(e) => eprintln!("Connection Error: {:?}", e),
}
})
.await;
pb.finish_with_message("done");
Ok(())
}
If you have any other suggestion, please let me know. This is essentially my first "real" code in Rust, so I might be doing things in some weird / wrong way.
Thanks in advance!