as a JS dev i'm having a real hard time understanding error handling in rust. especially with asyncronisity. heres some code from another stackoverflow question using async-std to call an async scraper function:
use std::fs::File;
use std::io::{self, BufReader};
use std::io::BufRead;
//use async_std::task;
async fn scrape<R: BufRead>(reader: &mut R) -> io::Result<()> {
reader.lines()
.try_for_each(|line_result| line_result.and_then(|line| Ok(println!("{}", line))))?;
Ok(())
}
fn main() {
let file_result = File::open("wlist.txt");
let file;
match file_result {
Ok(f) => file = f,
Err(e) => { println!("File open error! {}", e); return; },
}
let mut reader = BufReader::new(file);
match scrape(&mut reader) {
Ok(_) => println!("done!"),
Err(e) => println!("File read error! {}", e),
}
}
I'm trying to call scrape in an asyncronous way like:
task::block_on(match scrape(&mut reader) {
Ok(_) => println!("done!"),
Err(e) => println!("File read error! {}", e),
})
but it errors with
the trait std::future::Future is not implemented for ()
What's wrong with the code snippet? how can I run scraper as async with as less modification to the rest of the code as possible?