Agent skill
async-programming
Master Rust async/await with Tokio
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
# 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
Recommended Agent Skills
Expand your agent's capabilities with these related and highly-rated skills.
rust-wasm
Master WebAssembly with Rust - wasm-pack, wasm-bindgen, and browser integration
rust-docker
Master Docker containerization for Rust applications
rust-macros
Master Rust macros - declarative and procedural macros
trait-generics
Master Rust traits, generics, and type system
ownership-borrowing
Master Rust's ownership, borrowing, and lifetime system
rust-testing
Master Rust testing - unit tests, integration tests, mocking, and TDD
Didn't find tool you were looking for?