Implement async read and write in Rust by marking functions with `async` and using `await` on futures to handle non-blocking I/O.
Use the async keyword on functions and await on futures to implement non-blocking read and write operations.
use trpl::Html;
async fn page_title(url: &str) -> Option<String> {
let response_text = trpl::get(url).await.text().await;
Html::parse(&response_text)
.select_first("title")
.map(|title| title.inner_html())
}
In Cargo.toml, add the trpl dependency to enable these async operations:
[dependencies]
trpl = { path = "../../../packages/trpl" }
Async read and write lets your program start a task like downloading a file and do other work while waiting for it to finish, instead of freezing. Think of it like ordering food at a restaurant: you place the order (start the async task) and can chat with friends (do other work) while the kitchen prepares it, rather than staring at the kitchen door until it's ready. You use this pattern whenever your app needs to talk to the internet or disk without slowing down the user experience.