Rust Concurrency and Asynchronous Processing
Asyncronous
Async and Await (Future Trait)
This is just like promises. I learned a few things doing this. Renaming namespaces can be done using as. For the asynchronous work with thread still present there was a lot of crossover between the two. Note not all code listed so additional use statements demonstrate the renaming requirement.
use async_std::{prelude::*, io as async_io, task as async_task, fs as async_fs, fs::File as async_file};
use std::{cell::RefCell, fs, thread, sync::{mpsc::sync_channel, Arc, Mutex}};
async fn read_file(path: &str) -> async_io::Result<String> {
let mut file: async_file = async_fs::File::open(path).await?;
let mut contents = String::new();
file.read_to_string(&mut contents).await?;
Ok(contents)
}
fn main() {
let task = async_task::spawn(async {
let result = read_file("Cargo.toml").await;
match result {
Ok(k) => println!("contents {}", k),
Err(err) => println!("error {}", err),
}
});
async_std::task::block_on(task);
println!("Task stopped");
}