How to Use async and await in Rust

Use async to define non-blocking functions and await to pause execution until the result is ready.

Use the async keyword to define a function that returns a future and await to pause execution until an asynchronous operation completes.

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 main, you must run this function using an async runtime like tokio or async-std because main cannot be async by default in standard Rust.