mirror of https://github.com/KaiserY/trpl-zh-cn
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
25 lines
632 B
25 lines
632 B
extern crate trpl; // required for mdbook test
|
|
|
|
use trpl::Html;
|
|
|
|
// ANCHOR: run
|
|
fn main() {
|
|
let args: Vec<String> = std::env::args().collect();
|
|
|
|
trpl::run(async {
|
|
let url = &args[1];
|
|
match page_title(url).await {
|
|
Some(title) => println!("The title for {url} was {title}"),
|
|
None => println!("{url} had no title"),
|
|
}
|
|
})
|
|
}
|
|
// ANCHOR_END: run
|
|
|
|
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_element| title_element.inner_html())
|
|
}
|