Chuyển đến nội dung
Skillpedia
API và máy chủ (backend)18k · #10707

rust-expert

Skill này làm gì

Skill này hướng dẫn viết chương trình Rust, gồm quyền sở hữu và mượn (borrowing), xử lý lỗi, trait và mã bất đồng bộ với Tokio. Skill dành cho lập trình viên muốn phần mềm nhanh và an toàn về bộ nhớ.

Khi nào nên dùng

  • ✓Khi bạn đang viết hoặc xem lại mã Rust có xử lý quyền sở hữu hoặc vòng đời.
  • ✓Khi bạn cần chạy nhiều tác vụ bất đồng bộ đồng thời với Tokio.
  • ✓Khi bạn muốn mô hình hóa lỗi và trạng thái trong Rust bằng các kiểu dữ liệu rõ ràng.

Liên kết skill

https://github.com/RightNow-AI/openfang/blob/main/crates/openfang-skills/bundled/rust-expert/SKILL.md

Sao chép rồi dán vào AI của bạn (Claude, ChatGPT hoặc Cursor) và nhờ nó làm theo hướng dẫn.

Cách dùng

Cách dễ nhất: sao chép tin nhắn và dán vào AI của bạn. AI sẽ đọc skill và làm theo hướng dẫn.

Tin nhắn có sẵn để dán

Hãy dùng skill này để giúp tôi: https://github.com/RightNow-AI/openfang/blob/main/crates/openfang-skills/bundled/rust-expert/SKILL.md Đọc tệp SKILL.md tại liên kết đó và làm theo hướng dẫn cho việc của tôi.
Bạn muốn AI tự tìm những skill như thế này?Cài MCP Skillpedia miễn phí: chỉ một dòng, và AI sẽ tìm trong 15.000+ skill trước mỗi việc.Cài đặt trong 1 phút →

Ví dụ cách nhờ AI

“Hãy viết một hàm Rust đọc file cấu hình, trả về lỗi rõ ràng nếu file bị thiếu và nạp các giá trị vào một struct.”

5 skill mới mỗi tuần. Đọc chỉ 1 phút.

Cách hoạt động

Skill khuyến nghị dùng kiểu sở hữu ở ranh giới API, lỗi dựa trên Result tường minh, enum thay cho cờ (flag) và hạn chế tối đa unsafe, kèm lý do được ghi chép rõ.

Nội dung kỹ thuật (SKILL.md gốc)

Lấy nguyên văn từ GitHub, do tác giả viết bằng tiếng Anh.

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 thiserror for library errors and anyhow for 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; &self methods, the output borrows from self
  • Use tokio::spawn for concurrent tasks, tokio::select! for racing futures, and tokio::sync::mpsc for message passing between tasks
  • Prefer impl Trait in argument position for static dispatch and dyn Trait in 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 that Pin guarantees 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 FooBuilder with fn field(mut self, val: T) -> Self chainable setters and a fn build(self) -> Result<Foo> finalizer that validates invariants
  • Newtype Wrapper: Wrap String as struct UserId(String) to prevent accidental mixing of semantically different string types at the type level
  • RAII Guard: Implement Drop on 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 MutexGuard across an .await point; this can cause deadlocks since the guard is not Send across task suspension
  • Do not add unsafe blocks without a // SAFETY: comment explaining why the invariants are upheld; undocumented unsafe is a maintenance hazard
Câu hỏi thường gặp

Quyền sở hữu trong Rust là gì?

Đó là quy tắc mỗi giá trị chỉ có một chủ sở hữu, nhờ đó Rust giải phóng bộ nhớ an toàn mà không cần garbage collector.

Khi nào nên dùng mã unsafe?

Skill nói chỉ khi không có cách an toàn nào diễn đạt được thao tác đó, và mọi quy tắc an toàn phải được ghi lại.

Skill gợi ý thư viện xử lý lỗi nào?

Skill gợi ý thiserror cho lỗi trong thư viện và anyhow cho xử lý lỗi ở mức ứng dụng.

Chi tiết

Mô tả gốc của tác giả

Rust programming expert for ownership, lifetimes, async/await, traits, and unsafe code

repo: RightNow-AI/openfang

Độ phổ biến

Sao GitHub
18k
Vị trí xếp hạng
#10707
Giấy phép
Apache-2.0
Cập nhật
2/7/2026

Skill này có hữu ích không?

0

Đăng nhập để bình chọn.

Bình luận

  • …

Thêm skill từ kho mã này

Skill tương tự