diff options
| author | Ben Ray | 2026-05-03 17:02:04 -0400 |
|---|---|---|
| committer | Ben Ray | 2026-05-03 17:02:04 -0400 |
| commit | 364fde8e6c371731621a02ad984a144451428554 (patch) | |
| tree | 00aa91d290bbf4aec93c9c2373dc7c19bac6414a | |
| parent | 85899dfb2be18c7a74e20d539d0ca087030f2de1 (diff) | |
| -rw-r--r-- | hello-async/src/main.rs | 30 |
1 files changed, 29 insertions, 1 deletions
diff --git a/hello-async/src/main.rs b/hello-async/src/main.rs index e7a11a9..772bc7b 100644 --- a/hello-async/src/main.rs +++ b/hello-async/src/main.rs @@ -1,3 +1,31 @@ +use trpl::{Either, Html}; + fn main() { - println!("Hello, world!"); + let args: Vec<String> = std::env::args().collect(); + + trpl::block_on(async { + let title_fut_1 = page_title(&args[1]); + let title_fut_2 = page_title(&args[2]); + + let (url, maybe_title) = + match trpl::select(title_fut_1, title_fut_2).await { + Either::Left(left) => left, + Either::Right(right) => right, + }; + + println!("{url} returned first"); + match maybe_title { + Some(title) => println!("Its page title was: '{title}'"), + None => println!("It had no title"), + } + }) } + +async fn page_title(url: &str) -> (&str, Option<String>) { + let response_text = trpl::get(url).await.text().await; + let title = Html::parse(&response_text) + .select_first("title") + .map(|title| title.inner_html()); + (url, title) +} + |
