summaryrefslogtreecommitdiffstats
path: root/src/hello
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--src/hello-async/Cargo.toml7
-rw-r--r--src/hello-async/src/main.rs31
2 files changed, 38 insertions, 0 deletions
diff --git a/src/hello-async/Cargo.toml b/src/hello-async/Cargo.toml
new file mode 100644
index 0000000..8c4be20
--- /dev/null
+++ b/src/hello-async/Cargo.toml
@@ -0,0 +1,7 @@
+[package]
+name = "hello-async"
+version = "0.1.0"
+edition = "2024"
+
+[dependencies]
+trpl = "0.3.0"
diff --git a/src/hello-async/src/main.rs b/src/hello-async/src/main.rs
new file mode 100644
index 0000000..9d8a343
--- /dev/null
+++ b/src/hello-async/src/main.rs
@@ -0,0 +1,31 @@
+use std::env;
+use trpl::{Either, Html};
+
+fn main() {
+ let args: Vec<String> = 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)
+}