Agent skill

async-programming

Master Rust async/await with Tokio

Stars 1
Forks 0

Install this agent skill to your Project

npx add-skill https://github.com/pluginagentmarketplace/custom-plugin-rust/tree/main/skills/async-programming

SKILL.md

Async Programming Skill

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

Quick Start

Basic Setup

toml
# Cargo.toml
[dependencies]
tokio = { version = "1", features = ["full"] }
rust
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

rust
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

rust
// 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

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

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

Channels

rust
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

rust
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

rust
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

Didn't find tool you were looking for?

Be as detailed as possible for better results