Rust Async Patterns
Seth HobsonCommunityCreated by Seth Hobson
- Skill
- Guidelines
- Software development
- MIT
- No issues found
AutoGineer summary
Written by AutoGineer from the skill’s own files, not by the author
This skill provides a structured approach to asynchronous Rust development. It covers core abstractions like Futures, async functions, and the Tokio runtime, while offering patterns for concurrent task execution, inter-task communication via channels, and error handling strategies. It includes best practices for managing async state and avoiding common pitfalls such as blocking operations or deadlocks.
What it does
- Explains the Rust async execution model and the role of the runtime.
- Demonstrates concurrent task management using JoinSet and stream processing.
- Provides patterns for inter-task communication using mpsc, broadcast, oneshot, and watch channels.
- Outlines strategies for error propagation and custom error types in async contexts.
- Lists essential do's and don'ts for writing safe and effective async code.
- Offers examples for racing futures and implementing timeouts.
When to use it
- When building applications that require asynchronous I/O.
- When implementing concurrent network services in Rust.
- When you need to manage multiple concurrent tasks or streams.
- When debugging issues related to async execution or task synchronization.
- When establishing error handling patterns for async functions.
Advantages
- Provides clear, idiomatic patterns for common async scenarios.
- Helps avoid common async pitfalls like blocking the executor or holding locks across await points.
- Standardizes the use of channels for communication between concurrent tasks.
- Offers a structured approach to managing task lifecycles and concurrency limits.
Usage examples
- “How do I run multiple network requests concurrently in Rust?”
- The skill provides a pattern using JoinSet to spawn multiple tasks and collect their results, or using stream processing with buffer_unordered to enforce concurrency limits.
- “What is the best way to communicate between two async tasks?”
- The skill explains various channel types including mpsc for producer-consumer, broadcast for multi-consumer, and oneshot for single-value communication.
- “How should I handle errors in an async function?”
- The skill demonstrates using the anyhow crate for context-aware error propagation and defining custom error enums with thiserror for library-level code.
- “How can I race two async operations and take the first result?”
- The skill shows how to use the tokio::select! macro to wait on multiple futures and return the result of the one that completes first.
Install
Download the skill as a ZIP:
https://autogineer.com/agent-skills/wshobson/rust-async-patterns/downloadUnzip it into the cross-client skills folder of your project:
mkdir -p .agents/skills && unzip rust-async-patterns.zip -d .agents/skills
Documentation by Seth Hobson
Rust Async Patterns
Production patterns for async Rust programming with Tokio runtime, including tasks, channels, streams, and error handling.
When to Use This Skill
Building async Rust applications
Implementing concurrent network services
Using Tokio for async I/O
Handling async errors properly
Debugging async code issues
Optimizing async performance
Core Concepts
1. Async Execution Model
Future (lazy) → poll() → Ready(value) | Pending
↑ ↓
Waker ← Runtime schedules2. Key Abstractions
| Concept | Purpose |
|---|---|
Future | Lazy computation that may complete later |
async fn | Function returning impl Future |
await | Suspend until future completes |
Task | Spawned future running concurrently |
Runtime | Executor that polls futures |
Quick Start
# Cargo.toml
[dependencies]
tokio = { version = "1", features = ["full"] }
futures = "0.3"
async-trait = "0.1"
anyhow = "1.0"
tracing = "0.1"
tracing-subscriber = "0.3"use tokio::time::{sleep, Duration};
use anyhow::Result;
#[tokio::main]
async fn main() -> Result<()> {
// Initialize tracing
tracing_subscriber::fmt::init();
// Async operations
let result = fetch_data("https://api.example.com").await?;
println!("Got: {}", result);
Ok(())
}
async fn fetch_data(url: &str) -> Result<String> {
// Simulated async operation
sleep(Duration::from_millis(100)).await;
Ok(format!("Data from {}", url))
}Detailed patterns and worked examples
Detailed pattern documentation lives in references/details.md. Read that file when the navigation tier above is insufficient.
Best Practices
Do's
Use
tokio::select!- For racing futuresPrefer channels - Over shared state when possible
Use
JoinSet- For managing multiple tasksInstrument with tracing - For debugging async code
Handle cancellation - Check
CancellationToken
Don'ts
Don't block - Never use
std::thread::sleepin asyncDon't hold locks across awaits - Causes deadlocks
Don't spawn unboundedly - Use semaphores for limits
Don't ignore errors - Propagate with
?or logDon't forget Send bounds - For spawned futures
Files
- ATTRIBUTION.mdAdded by AutoGineer995 B
- LICENSELicence file added by AutoGineer1.0 KiB
- SKILL.md2.6 KiB
- references/details.md9.7 KiB
Requirements and compatibility
Works with: Claude Code, Claude apps, OpenAI Codex, GitHub Copilot, Cursor, Gemini CLI, Kiro, OpenCode, Goose, Amp, Roo Code, JetBrains Junie, Factory Droid, Devin (ex-Windsurf), Mistral Vibe, OpenHands, Letta
“Works with” means the tool reads the open Agent Skills format; a skill written for one tool may use features others lack.
Security
No issues found by our static scan· No issues found
A static scan is not a guarantee. Skills are third-party content: review a skill before you let an agent use it.
Attribution and licence
- Created by
- Seth Hobson
- Publisher
- Seth Hobson (Community)
- Source
- wshobson/agents · plugins/systems-programming/skills/rust-async-patterns · commit 4bde316
- Licence
- MIT License (redistribution allowed)
- LICENSE (added by AutoGineer from LICENSE)
- ATTRIBUTION.md (written by AutoGineer)
- Synced
AutoGineer is not affiliated with or endorsed by Seth Hobson or Seth Hobson.
Download ZIP
The ZIP holds the skill’s files, its licence files and an ATTRIBUTION.md. Download rust-async-patterns.zip