dragonbitestail

Learning Rust Async

Aug 25th, 2025 (edited)
356
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Rust 3.92 KB | Source Code | 0 0
  1. use std::time::Duration;
  2.  
  3. // Requires:
  4. // cargo add trpl
  5. // Warn: pulls in large number of dependencies
  6. // Reminder: cargo clean
  7.  
  8. fn main() {
  9.     // https://doc.rust-lang.org/book/ch17-02-concurrency-with-async.html#listing-17-9
  10.     trpl::run(async {
  11.         let (tx, mut rx) = trpl::channel();
  12.  
  13.         let val = String::from("hi");
  14.         tx.send(val).unwrap();
  15.  
  16.         let received = rx.recv().await.unwrap();
  17.         println!("Got: {received}");
  18.     });
  19.  
  20.     // https://doc.rust-lang.org/book/ch17-02-concurrency-with-async.html#listing-17-10
  21.     // Everything runs sequentially--only 1 outer async.
  22.     trpl::run(async {
  23.         let (tx, mut rx) = trpl::channel();
  24.  
  25.         let vals = vec![
  26.             String::from("hi"),
  27.             String::from("from"),
  28.             String::from("the"),
  29.             String::from("future"),
  30.         ];
  31.  
  32.         for val in vals {
  33.             tx.send(val).unwrap();
  34.             trpl::sleep(Duration::from_millis(500)).await;
  35.         }
  36.         // Force tx to close so rx.recv() gets None after all messages delivered.
  37.         drop(tx);
  38.  
  39.         while let Some(value) = rx.recv().await {
  40.             println!("Got: {value}");
  41.         }
  42.     });
  43.  
  44.     // https://doc.rust-lang.org/book/ch17-02-concurrency-with-async.html#listing-17-11
  45.     // Then
  46.     // https://doc.rust-lang.org/book/ch17-02-concurrency-with-async.html#listing-17-12a
  47.     // where `move` was added for the tx_fut async block.  This allows tx to drop automatically
  48.     // when it goes out of scope.
  49.     //
  50.     // Each tx mesage is sent w/ 500 ms delay as intended as a lazy defined future
  51.     // which runs in the trpl::join().
  52.     // rx.recv() no longer blocks forever as tx is dropped after the for loop is done sending
  53.     // val messages
  54.     trpl::run(async {
  55.         let (tx, mut rx) = trpl::channel();
  56.  
  57.         let tx_fut = async move {
  58.             let vals = vec![
  59.                 String::from("hi"),
  60.                 String::from("from"),
  61.                 String::from("the"),
  62.                 String::from("future"),
  63.             ];
  64.  
  65.             for val in vals {
  66.                 tx.send(val).unwrap();
  67.                 trpl::sleep(Duration::from_millis(500)).await;
  68.             }
  69.         };
  70.  
  71.         let rx_fut = async {
  72.             while let Some(value) = rx.recv().await {
  73.                 println!("received '{value}'");
  74.             }
  75.         };
  76.  
  77.         trpl::join(tx_fut, rx_fut).await;
  78.     });
  79.  
  80.     // https://doc.rust-lang.org/book/ch17-02-concurrency-with-async.html#listing-17-13
  81.     // Like 17-12, except with multiple async tx_fut's
  82.     // Then used join! macro in place of join3 per
  83.     // https://doc.rust-lang.org/book/ch17-03-more-futures.html#listing-17-14
  84.     // TODO: convert code to use join_all() in place of join! macro
  85.     trpl::run(async {
  86.         let (tx, mut rx) = trpl::channel();
  87.  
  88.         let tx1 = tx.clone();
  89.         let tx1_fut = async move {
  90.             let vals = vec![
  91.                 String::from("hi"),
  92.                 String::from("from"),
  93.                 String::from("the"),
  94.                 String::from("future"),
  95.             ];
  96.  
  97.             for val in vals {
  98.                 tx1.send(val).unwrap();
  99.                 trpl::sleep(Duration::from_millis(500)).await;
  100.             }
  101.         };
  102.  
  103.         let tx_fut = async move {
  104.             let vals = vec![
  105.                 String::from("more"),
  106.                 String::from("messages"),
  107.                 String::from("for"),
  108.                 String::from("you"),
  109.             ];
  110.  
  111.             for val in vals {
  112.                 tx.send(val).unwrap();
  113.                 trpl::sleep(Duration::from_millis(1500)).await;
  114.             }
  115.         };
  116.  
  117.         let rx_fut = async {
  118.             while let Some(value) = rx.recv().await {
  119.                 println!("received '{value}'");
  120.             }
  121.         };
  122.  
  123.         //trpl::join3(tx1_fut, tx_fut, rx_fut).await;
  124.         trpl::join!(tx1_fut, tx_fut, rx_fut);
  125.     });
  126. }
Tags: rust
Advertisement
Add Comment
Please, Sign In to add comment