summaryrefslogtreecommitdiffstats
path: root/src/hello-async
diff options
context:
space:
mode:
authorhybrid <hybrid@hybridlabs.cc>2026-10-31 03:54:43 +0300
committerhybrid <hybrid@hybridlabs.cc>2026-10-31 03:54:43 +0300
commit62dcfb1d9f95503f8eb8be1b29fa1d75bfcc27e2 (patch)
treeb8461eda6cce3f9bbfb2261dd828077e17c7d226 /src/hello-async
parent18ad654357d6cca34782aaa269f5e8e06cbe9241 (diff)
downloadrust-book-62dcfb1d9f95503f8eb8be1b29fa1d75bfcc27e2.tar.gz
rust-book-62dcfb1d9f95503f8eb8be1b29fa1d75bfcc27e2.tar.bz2
rust-book-62dcfb1d9f95503f8eb8be1b29fa1d75bfcc27e2.zip
add: 7 craits
smartpointers interior_mutability threads mutex hello-async async_concurrency multi_futures
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)
+}