async-programming

Master Rust async/await with Tokio

Safety Notice

This listing is imported from skills.sh public index metadata. Review upstream SKILL.md and repository scripts before running.

Copy this and send it to your AI assistant to learn

Install skill "async-programming" with this command: npx skills add pluginagentmarketplace/custom-plugin-rust/pluginagentmarketplace-custom-plugin-rust-async-programming

Async Programming Skill

Master Rust's asynchronous programming with async/await and Tokio.

Quick Start

Basic Setup

# Cargo.toml
[dependencies]
tokio = { version = "1", features = ["full"] }
use tokio;

#[tokio::main]
async fn main() {
    let result = fetch_data().await;
    println!("{:?}", result);
}

async fn fetch_data() -> String {
    // Async operation
    tokio::time::sleep(tokio::time::Duration::from_secs(1)).await;
    "Data fetched".to_string()
}

Spawning Tasks

use tokio;

#[tokio::main]
async fn main() {
    // Spawn concurrent task
    let handle = tokio::spawn(async {
        expensive_operation().await
    });

    // Do other work...

    // Wait for result
    let result = handle.await.unwrap();
}

Concurrent Execution

// Run multiple futures concurrently
let (r1, r2, r3) = tokio::join!(
    fetch_user(1),
    fetch_user(2),
    fetch_user(3),
);

// Race futures - first to complete wins
tokio::select! {
    result = operation_a() => println!("A: {:?}", result),
    result = operation_b() => println!("B: {:?}", result),
}

Common Patterns

Timeout

use tokio::time::{timeout, Duration};

match timeout(Duration::from_secs(5), slow_operation()).await {
    Ok(result) => println!("Completed: {:?}", result),
    Err(_) => println!("Timed out"),
}

Channels

use tokio::sync::mpsc;

let (tx, mut rx) = mpsc::channel(32);

tokio::spawn(async move {
    tx.send("message").await.unwrap();
});

while let Some(msg) = rx.recv().await {
    println!("Got: {}", msg);
}

Mutex

use std::sync::Arc;
use tokio::sync::Mutex;

let data = Arc::new(Mutex::new(0));
let data_clone = Arc::clone(&data);

tokio::spawn(async move {
    let mut lock = data_clone.lock().await;
    *lock += 1;
});

HTTP Client Example

use reqwest;

#[tokio::main]
async fn main() -> Result<(), reqwest::Error> {
    let response = reqwest::get("https://api.example.com/data")
        .await?
        .json::<serde_json::Value>()
        .await?;

    println!("{:?}", response);
    Ok(())
}

Resources

Source Transparency

This detail page is rendered from real SKILL.md content. Trust labels are metadata-based hints, not a safety guarantee.

Related Skills

Related by shared tags or category signals.

Automation

error-handling

No summary provided by upstream source.

Repository SourceNeeds Review
Automation

rust-wasm

No summary provided by upstream source.

Repository SourceNeeds Review