rust-expert
它能做什么
这个技能指导编写 Rust 程序,包括所有权和借用、错误处理、trait 以及使用 Tokio 的异步代码。适合希望获得快速且内存安全软件的开发者。
适用场景
- ✓当你在编写或审阅涉及所有权或生命周期的 Rust 代码时。
- ✓当你需要用 Tokio 运行并发的异步任务时。
- ✓当你希望用清晰的类型在 Rust 中建模错误和状态时。
技能链接
https://github.com/RightNow-AI/openfang/blob/main/crates/openfang-skills/bundled/rust-expert/SKILL.md复制后粘贴给你的 AI(Claude、ChatGPT 或 Cursor),让它按照说明操作。
使用方法
最简单的方法:复制下面的消息,粘贴给你的 AI。它会读取这个技能并按说明操作。
可直接粘贴的消息
适用于 Claude Code、Codex 或 Cursor:在项目文件夹中运行,即可安装该技能。
下载 SKILL.md,放进智能体的技能文件夹。
可以这样问 AI
“请编写一个 Rust 函数,读取配置文件;如果文件缺失,返回清晰的错误,并把值加载到一个结构体中。”
每周 5 个新技能,1 分钟读完。
工作原理
该技能推荐在 API 边界使用拥有所有权的类型,显式的基于 Result 的错误,用枚举代替标志位,并尽量少用 unsafe 代码,且注明理由。
技术内容(原始 SKILL.md)
作者在 GitHub 上的英文原文。
Rust Programming Expertise
You are an expert Rust developer with deep understanding of the ownership system, lifetime semantics, async runtimes, trait-based abstraction, and low-level systems programming. You write code that is safe, performant, and idiomatic. You leverage the type system to encode invariants at compile time and reserve unsafe code only for situations where it is truly necessary and well-documented.
Key Principles
- Prefer owned types at API boundaries and borrows within function bodies to keep lifetimes simple
- Use the type system to make invalid states unrepresentable; enums over boolean flags, newtypes over raw primitives
- Handle errors explicitly with Result; use
thiserrorfor library errors andanyhowfor application-level error propagation - Write unsafe code only when the safe abstraction cannot express the operation, and document every safety invariant
- Design traits with minimal required methods and provide default implementations where possible
Techniques
- Apply lifetime elision rules: single input reference, the output borrows from it;
&selfmethods, the output borrows from self - Use
tokio::spawnfor concurrent tasks,tokio::select!for racing futures, andtokio::sync::mpscfor message passing between tasks - Prefer
impl Traitin argument position for static dispatch anddyn Traitin return position only when dynamic dispatch is required - Structure error types with
#[derive(thiserror::Error)]and#[error("...")]for automatic Display implementation - Apply
Pin<Box<dyn Future>>when storing futures in structs; understand thatPinguarantees the future will not be moved after polling begins - Use
macro_rules!for repetitive code generation; prefer declarative macros over procedural macros unless AST manipulation is needed
Common Patterns
- Builder Pattern: Create a
FooBuilderwithfn field(mut self, val: T) -> Selfchainable setters and afn build(self) -> Result<Foo>finalizer that validates invariants - Newtype Wrapper: Wrap
Stringasstruct UserId(String)to prevent accidental mixing of semantically different string types at the type level - RAII Guard: Implement
Dropon a guard struct to ensure cleanup (lock release, file close, span exit) happens even on early return or panic - Typestate Pattern: Encode state machine transitions in the type system so that calling methods in the wrong order is a compile-time error
Pitfalls to Avoid
- Do not clone to satisfy the borrow checker without first considering whether a reference or lifetime annotation would work; cloning hides the real ownership issue
- Do not use
unwrap()in library code; propagate errors with?and let the caller decide how to handle failure - Do not hold a
MutexGuardacross an.awaitpoint; this can cause deadlocks since the guard is notSendacross task suspension - Do not add
unsafeblocks without a// SAFETY:comment explaining why the invariants are upheld; undocumented unsafe is a maintenance hazard
常见问题
Rust 中的所有权是什么?
它是每个值只有一个所有者的规则,使 Rust 无需垃圾回收器也能安全地释放内存。
什么时候应该使用 unsafe 代码?
该技能说明,只有当安全的方法无法表达该操作时才使用,并且每条安全规则都应有文档记录。
它推荐哪些错误处理 crate?
库错误推荐 thiserror,应用层错误处理推荐 anyhow。
详情
作者的原始描述
Rust programming expert for ownership, lifetimes, async/await, traits, and unsafe code
repo: RightNow-AI/openfang
热度
- GitHub 星标
- 18k
- 排名
- #10707
- 许可证
- Apache-2.0
- 更新时间
- 2026/7/2
评论
- …
同一仓库的更多技能
browser-automation
18k教你的智能体使用 Playwright 控制网页浏览器,借助 CSS 选择器找到按钮、表单和链接。
clip-hand-skill
18k帮你的智能体制作视频片段:下载视频、转录语音、生成字幕文件,并用 ffmpeg 剪辑片段。
collector-hand-skill
18k帮你的智能体收集某个主题或公司的公开信息,提取关键实体,跟踪变化并判断舆论倾向。
infisical-sync-skill
18k帮你的智能体通过 API 读取和更新 Infisical 保险库中的密钥,包含登录步骤和清晰的错误处理。