How to use tokio oneshot channel

Create a one-time message channel in Rust using tokio::sync::oneshot::channel() to send data between async tasks.

Use tokio::sync::oneshot::channel() to create a sender and receiver pair for sending a single message between tasks. The sender transmits the value once, and the receiver awaits it, returning a Result that you must unwrap or handle.

use tokio::sync::oneshot;

#[tokio::main]
async fn main() {
    let (tx, rx) = oneshot::channel();
    
    tokio::spawn(async move {
        let msg = "Hello from the sender";
        let _ = tx.send(msg);
    });

    let received = rx.await.unwrap();
    println!("Got: {received}");
}