Rust Skills - Agent Instructions

2026-08-30 浏览 (1)

Rust Skills - Agent Instructions

For OpenAI Codex and compatible agents

Default Project Settings

When creating Rust projects or Cargo.toml files, ALWAYS use:

[package]
edition = "2024"
rust-version = "1.85"

[lints.rust]
unsafe_code = "warn"

[lints.clippy]
all = "warn"
pedantic = "warn"

Core Capabilities

1. Question Routing

Route Rust questions to appropriate skills:

  • Ownership/borrowing → m01-ownership
  • Smart pointers → m02-resource
  • Error handling → m06-error-handling
  • Concurrency → m07-concurrency
  • Unsafe code → unsafe-checker

2. Code Style

Follow Rust coding guidelines:

  • Use snake_case for variables and functions
  • Use PascalCase for types and traits
  • Use SCREAMING_SNAKE_CASE for constants
  • Max line length: 100 characters
  • Use ? operator instead of unwrap() in library code

3. Error Handling

// Good: Use Result with context
fn read_config() -> Result<Config, ConfigError> {
    let content = std::fs::read_to_string("config.toml")
        .map_err(|e| ConfigError::Io(e))?;
    toml::from_str(&content)
        .map_err(|e| ConfigError::Parse(e))
}

// Avoid: unwrap() in library code
fn read_config() -> Config {
    let content = std::fs::read_to_string("config.toml").unwrap(); // Bad
    toml::from_str(&content).unwrap() // Bad
}

4. Unsafe Code

Every unsafe block MUST have a // SAFETY: comment:

// SAFETY: We checked that index < len above, so this is in bounds
unsafe { slice.get_unchecked(index) }

5. Common Error Fixes

ErrorCauseFix
E0382Use of moved valueClone, borrow, or use reference
E0597Lifetime too shortExtend lifetime or restructure
E0502Borrow conflictSplit borrows or use RefCell
E0499Multiple mut borrowsRestructure to single mut borrow
E0277Missing trait implAdd trait bound or implement trait

Quick Reference

Ownership

  • Each value has one owner
  • Borrowing: &T (shared) or &mut T (exclusive)
  • Lifetimes: 'a annotations for references

Smart Pointers

  • Box<T>: Heap allocation
  • Rc<T>: Reference counting (single-threaded)
  • Arc<T>: Atomic reference counting (thread-safe)
  • RefCell<T>: Interior mutability

Concurrency

  • Send: Safe to transfer between threads
  • Sync: Safe to share references between threads
  • Mutex<T>: Mutual exclusion
  • RwLock<T>: Reader-writer lock

Async

#[tokio::main]
async fn main() {
    let handle = tokio::spawn(async {
        // async work
    });
    handle.await.unwrap();
}

Skill Files

For detailed guidance, see:

  • skills/rust-router/SKILL.md - Question routing
  • skills/coding-guidelines/SKILL.md - Code style rules
  • skills/unsafe-checker/SKILL.md - Unsafe code review
  • skills/m01-ownership/SKILL.md - Ownership concepts
  • skills/m06-error-handling/SKILL.md - Error patterns
  • skills/m07-concurrency/SKILL.md - Concurrency patterns

你可能感兴趣的文章

Rust Skills - Claude Instructions

Rust Skills

Rust Skills Trigger Test Checklist

Rust Skills Review Report

Rust Skills

Rust Skills

Changelog

  • 所属分类: AI
  • 本文标签: rust
  • 版权声明: 本文链接 https://seaxiang.com/blog/uOMqE7LN