2.9 KiB
2.9 KiB
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 ofunwrap()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
| Error | Cause | Fix |
|---|---|---|
| E0382 | Use of moved value | Clone, borrow, or use reference |
| E0597 | Lifetime too short | Extend lifetime or restructure |
| E0502 | Borrow conflict | Split borrows or use RefCell |
| E0499 | Multiple mut borrows | Restructure to single mut borrow |
| E0277 | Missing trait impl | Add trait bound or implement trait |
Quick Reference
Ownership
- Each value has one owner
- Borrowing:
&T(shared) or&mut T(exclusive) - Lifetimes:
'aannotations for references
Smart Pointers
Box<T>: Heap allocationRc<T>: Reference counting (single-threaded)Arc<T>: Atomic reference counting (thread-safe)RefCell<T>: Interior mutability
Concurrency
Send: Safe to transfer between threadsSync: Safe to share references between threadsMutex<T>: Mutual exclusionRwLock<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 routingskills/coding-guidelines/SKILL.md- Code style rulesskills/unsafe-checker/SKILL.md- Unsafe code reviewskills/m01-ownership/SKILL.md- Ownership conceptsskills/m06-error-handling/SKILL.md- Error patternsskills/m07-concurrency/SKILL.md- Concurrency patterns