跳到正文
Skillpedia
通用编程41k · #6800

rust-async-patterns

它能做什么

这个技能可以帮你用 Rust 编写异步代码(async)。它会讲解如何在不让程序卡住的情况下同时处理多件事,并介绍 Tokio 等工具的使用。

适用场景

  • ✓开发需要同时处理多项工作的 Rust 程序时。
  • ✓构建需要处理大量连接的网络系统时。
  • ✓调试或优化 Rust 异步代码的性能时。

技能链接

https://github.com/wshobson/agents/blob/main/plugins/systems-programming/skills/rust-async-patterns/SKILL.md

复制后粘贴给你的 AI(Claude、ChatGPT 或 Cursor),让它按照说明操作。

使用方法

最简单的方法:复制下面的消息,粘贴给你的 AI。它会读取这个技能并按说明操作。

可直接粘贴的消息

请用这个技能帮我:https://github.com/wshobson/agents/blob/main/plugins/systems-programming/skills/rust-async-patterns/SKILL.md 阅读该链接中的 SKILL.md 文件,并在我的任务中按照它的说明来做。
想让你的 AI 自己找到这样的技能吗?安装免费的 Skillpedia MCP:只需一行,每次任务前它都会在 15,000+ 个技能中搜索。1 分钟完成安装 →

可以这样问 AI

“如何让我的 Rust 程序并发等待两件事情,并拿到最先完成的那一个的结果?”

每周 5 个新技能,1 分钟读完。

工作原理

它会讲解 Rust 异步代码的基本概念,展示“任务”如何并行运行,以及如何使用“future”(可以在稍后完成的计算)。

技术内容(原始 SKILL.md)

作者在 GitHub 上的英文原文。

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

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 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
常见问题

Rust 中的 async 是什么?

它是一种编写代码的方式,程序在等待耗时操作(比如网络响应)时可以去做其他事情,而不会让整个程序卡住。

什么是 Tokio?

它是 Rust 中很流行的工具,可以帮助高效地管理和运行异步代码。

Rust 异步编程中的“任务”是什么?

任务是可以独立运行、并与其他代码同时执行的异步代码片段。

详情

作者的原始描述

Master Rust async programming with Tokio, async traits, error handling, and concurrent patterns. Use when building async Rust applications, implementing concurrent systems, or debugging async code.

repo: wshobson/agents

热度

GitHub 星标
41k
排名
#6800
许可证
MIT
更新时间
2026/10/15

这个技能有用吗?

0

登录 后即可投票。

评论

  • …

同一仓库的更多技能

相似技能