rust
4
总安装量
2
周安装量
#54155
全站排名
安装命令
npx skills add https://github.com/poletron/custom-rules --skill rust
Agent 安装分布
github-copilot
2
mcpjam
1
claude-code
1
zencoder
1
crush
1
cline
1
Skill 文档
Critical Patterns
Error Handling (REQUIRED)
// â
ALWAYS: Use Result with ? operator
fn read_config(path: &str) -> Result<Config, ConfigError> {
let content = std::fs::read_to_string(path)?;
let config: Config = serde_json::from_str(&content)?;
Ok(config)
}
// â NEVER: Use unwrap in production
let config = std::fs::read_to_string(path).unwrap();
Ownership Patterns (REQUIRED)
// â
ALWAYS: Prefer borrowing over ownership transfer
fn process(data: &[u8]) -> Result<(), Error> { ... }
// â
Use Clone only when necessary
fn take_ownership(data: Vec<u8>) -> Vec<u8> { ... }
// â NEVER: Unnecessary cloning
process(data.clone()); // If borrow works, use it
Option Handling (REQUIRED)
// â
ALWAYS: Pattern match or use combinators
let name = user.name.as_ref().unwrap_or(&default_name);
// â
Use if let for single variant
if let Some(name) = user.name {
println!("Hello, {}", name);
}
// â NEVER: Blind unwrap
let name = user.name.unwrap();
Decision Tree
Need to modify data? â Use &mut reference
Need to read data? â Use & reference
Need to transfer? â Move ownership
Need cleanup? â Implement Drop trait
Need async? â Use tokio or async-std
Need error context? â Use anyhow or thiserror
Code Examples
Struct with Lifetimes
struct Parser<'a> {
input: &'a str,
position: usize,
}
impl<'a> Parser<'a> {
fn new(input: &'a str) -> Self {
Self { input, position: 0 }
}
}
Async with Tokio
use tokio;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let response = reqwest::get("https://api.example.com/data")
.await?
.json::<Response>()
.await?;
Ok(())
}
Commands
cargo new myapp # Create new project
cargo build --release # Build optimized
cargo run # Build and run
cargo test # Run tests
cargo clippy # Linting
cargo fmt # Format code
Resources
- Ownership & Borrowing: ownership-borrowing.md
- Async Programming: async-programming.md
- Best Practices: best-practices.md