首页/AI 后端工程/rust-async-patterns
R

rust-async-patterns

by @wshobsonv
4.5(120)

此技能提供 Rust 异步编程的生产级模式,涵盖 Tokio 运行时、任务、通道、流和错误处理。它能帮助开发者构建高性能、高并发的异步 Rust 应用,有效处理并发服务中的错误,并优化代码性能,从而提升开发效率和系统稳定性。

rustasynctokioconcurrencybackendGitHub
安装方式
npx skills add https://github.com/wshobson/agents --skill rust-async-patterns
compare_arrows

Before / After 效果对比

2
使用前

在没有明确异步模式指导下,Rust 开发者常面临复杂的并发问题调试、死锁和性能瓶颈,导致开发周期延长和系统不稳定。

使用后

通过应用这些经过验证的异步模式,开发者能显著减少调试时间,避免常见的并发问题,并以更高的信心和效率构建健壮、高性能的 Rust 服务。

SKILL.md

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 schedules

2. Key Abstractions

ConceptPurpose
FutureLazy computation that may complete later
async fnFunction returning impl Future
awaitSuspend until future completes
TaskSpawned future running concurrently
RuntimeExecutor 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 futures
  • Prefer channels - Over shared state when possible
  • Use JoinSet - For managing multiple tasks
  • Instrument with tracing - For debugging async code
  • Handle cancellation - Check CancellationToken

Don'ts

  • Don't block - Never use std::thread::sleep in async
  • Don't hold locks across awaits - Causes deadlocks
  • Don't spawn unboundedly - Use semaphores for limits
  • Don't ignore errors - Propagate with ? or log
  • Don't forget Send bounds - For spawned futures

用户评价 (0)

发表评价

效果
易用性
文档
兼容性

暂无评价

统计数据

安装量15.4K
评分4.5 / 5.0
版本
更新日期2026年7月8日
对比案例2 组

用户评分

4.5(120)
5
37%
4
43%
3
13%
2
5%
1
2%

为此 Skill 评分

0.0

兼容平台

🤖claude-code

时间线

创建2026年5月29日
最后更新2026年7月8日
🎁 Agent 知识卡片
调研问卷