main
Rust Skill
Modern Rust development guidance using Cargo, Clippy, and best practices.
Overview
This skill provides comprehensive guidance for Rust development following 2025 best practices:
- Cargo: Build system and package manager
- Clippy: Over 550 lints for catching mistakes
- rustfmt: Official code formatter
- Criterion: Statistical benchmarking
- thiserror/anyhow: Modern error handling
Workflows
Building & Testing
- Build: Compile projects with various profiles and targets
- Test: Unit, integration, and property-based testing
- Bench: Performance benchmarking with Criterion
Code Quality
Dependencies & Publishing
- Deps: Manage dependencies with Cargo
- Workspace: Multi-crate monorepos
- Publish: Publish crates to crates.io
Quick Start
New Project
# Create binary
cargo new myapp
# Create library
cargo new --lib mylib
cd myapp
cargo build
cargo run
Add Dependencies
cargo add serde tokio anyhow
cargo add --dev proptest criterion
Quality Checks
# Format code
cargo fmt
# Lint code
cargo clippy --all-targets --all-features -- -D warnings
# Run tests
cargo test
# Run benchmarks
cargo bench
Key Concepts
Ownership & Borrowing
Rust’s ownership system ensures memory safety without garbage collection:
fn main() {
let s = String::from("hello"); // s owns the string
takes_ownership(s); // s moved, no longer valid
// println!("{}", s); // Would error!
let x = 5;
makes_copy(x); // i32 is Copy, x still valid
println!("{}", x); // OK!
}
Error Handling
Use Result and Option instead of exceptions:
use anyhow::{Context, Result};
fn process_file(path: &str) -> Result<()> {
let contents = std::fs::read_to_string(path)
.context("Failed to read file")?;
// Process contents...
Ok(())
}
Zero-Cost Abstractions
High-level code compiles to efficient machine code:
// Iterator chain - as fast as hand-written loop
let sum: i32 = numbers
.iter()
.filter(|&&x| x % 2 == 0)
.map(|&x| x * 2)
.sum();
Best Practices
- Use Clippy: Run before every commit
- Format with rustfmt: Consistent style
- Handle errors explicitly: No
unwrap()in libraries - Write tests: Unit and integration tests
- Document public APIs: Use
///doc comments - Follow API Guidelines: See Rust API Guidelines
- Use iterators: Functional and efficient
- Leverage type system: Make invalid states unrepresentable
Modern Rust (2024/2025)
Edition 2021
- Disjoint capture in closures
- IntoIterator for arrays
- Panic macro consistency
Upcoming Features
- Async traits (stabilized)
- Generic associated types (GATs)
- Let-else statements
- Edition 2024 (coming soon)