This Rust code demonstrates basic asynchronous task spawning using the Tokio runtime. Here's what it does:
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();
}It creates and executes an asynchronous task in a separate "green thread" (Tokio task) and waits for it to complete.
-
#[tokio::main]- This attribute macro transforms themainfunction into an asynchronous runtime. It essentially wraps your code with the necessary setup to run async code, creating a Tokio runtime behind the scenes. -
async fn main()- Declares an asynchronous main function (which is normally not allowed in Rust, but the macro above makes it possible). -
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. -
handle- The spawn function returns aJoinHandle, which is a handle to the spawned task. You can use this to wait for the task or retrieve its result. -
handle.await.unwrap()- Waits for the spawned task to complete:.awaitpauses execution until the task finishes.unwrap()extracts the result (panics if the task panicked)
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.