Skip to content

Latest commit

 

History

History
41 lines (26 loc) · 1.72 KB

File metadata and controls

41 lines (26 loc) · 1.72 KB

Tokio Async Function Explanation

This Rust code demonstrates basic asynchronous task spawning using the Tokio runtime. Here's what it does:

Code Example

use tokio;
#[tokio::main]
async fn main() {
    let handle = tokio::spawn(async {
        println!("Hello from spawned task!");
    });
    
    // Wait for the task to complete
    handle.await.unwrap();
}

Overall Purpose

It creates and executes an asynchronous task in a separate "green thread" (Tokio task) and waits for it to complete.

Breaking it down

  1. #[tokio::main] - This attribute macro transforms the main function into an asynchronous runtime. It essentially wraps your code with the necessary setup to run async code, creating a Tokio runtime behind the scenes.

  2. async fn main() - Declares an asynchronous main function (which is normally not allowed in Rust, but the macro above makes it possible).

  3. tokio::spawn(async { ... }) - Spawns a new asynchronous task that runs concurrently. The task is given an async block that prints a message. This task is scheduled on the Tokio runtime's thread pool and can run independently.

  4. handle - The spawn function returns a JoinHandle, which is a handle to the spawned task. You can use this to wait for the task or retrieve its result.

  5. handle.await.unwrap() - Waits for the spawned task to complete:

    • .await pauses execution until the task finishes
    • .unwrap() extracts the result (panics if the task panicked)

In Simple Terms

This program starts an async task that prints "Hello from spawned task!", then waits for that task to finish before the program exits. While simple here, this pattern is fundamental for concurrent programming in Rust with Tokio.