To convert sync code to async in Rust, add the async keyword to the function signature and use .await on any asynchronous 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 main, you must run the async function using a runtime like tokio:
#[tokio::main]
async fn main() {
let title = page_title("https://example.com").await;
println!("{title:?}");
}