rust-coding-standards

Master Rust's ownership system, type safety, and zero-cost abstractions for building safe, concurrent, and performant systems. Covers borrowing, lifetimes, traits, error handling, async/await, and testing patterns.

Best use case

rust-coding-standards is best used when you need a repeatable AI agent workflow instead of a one-off prompt.

Master Rust's ownership system, type safety, and zero-cost abstractions for building safe, concurrent, and performant systems. Covers borrowing, lifetimes, traits, error handling, async/await, and testing patterns.

Teams using rust-coding-standards should expect a more consistent output, faster repeated execution, less prompt rewriting.

When to use this skill

  • You want a reusable workflow that can be run more than once with consistent structure.

When not to use this skill

  • You only need a quick one-off answer and do not need a reusable workflow.
  • You cannot install or maintain the underlying files, dependencies, or repository context.

Installation

Claude Code / Cursor / Codex

$curl -o ~/.claude/skills/rust/SKILL.md --create-dirs "https://raw.githubusercontent.com/williamzujkowski/standards/main/skills/coding-standards/rust/SKILL.md"

Manual Installation

  1. Download SKILL.md from GitHub
  2. Place it in .claude/skills/rust/SKILL.md inside your project
  3. Restart your AI agent — it will auto-discover the skill

How rust-coding-standards Compares

Feature / Agentrust-coding-standardsStandard Approach
Platform SupportNot specifiedLimited / Varies
Context Awareness High Baseline
Installation ComplexityUnknownN/A

Frequently Asked Questions

What does this skill do?

Master Rust's ownership system, type safety, and zero-cost abstractions for building safe, concurrent, and performant systems. Covers borrowing, lifetimes, traits, error handling, async/await, and testing patterns.

Where can I find the source code?

You can find the source code on GitHub using the link provided at the top of the page.

SKILL.md Source

# Rust Coding Standards

> **Purpose**: Master Rust's ownership system, type safety, and zero-cost abstractions for building safe, concurrent, and performant systems.

## Level 1: Quick Reference

### Core Principles

- **Ownership**: Each value has exactly one owner
- **Borrowing**: References allow multiple readers XOR one writer
- **Zero-cost abstractions**: High-level code compiles to optimal machine code
- **Explicit over implicit**: No hidden allocations or control flow

### Ownership & Borrowing Cheat Sheet

```rust
// Ownership transfer (move)
let s1 = String::from("hello");
let s2 = s1; // s1 is now invalid

// Immutable borrow (multiple allowed)
let s = String::from("hello");
let r1 = &s;
let r2 = &s;
println!("{} and {}", r1, r2); // OK

// Mutable borrow (exclusive)
let mut s = String::from("hello");
let r1 = &mut s;
r1.push_str(" world");

// Copy types (stack-only)
let x = 5;
let y = x; // x is still valid (i32 implements Copy)
```

### Result & Option

```rust
// Option: value may be absent
let name = find_user(42).map(|u| u.name).unwrap_or("Anonymous".into());

// Early return with ?
fn process() -> Result<(), Error> {
    let config = parse_config("app.toml")?;
    Ok(())
}
```

### Essential Checklist

- [ ] `cargo clippy -- -D warnings`
- [ ] `cargo fmt -- --check`
- [ ] `cargo test`
- [ ] `cargo doc --no-deps`
- [ ] `cargo audit`
- [ ] Document all `unsafe` blocks

### Memory Safety Rules

1. **No data races**: Compiler enforces at compile time
2. **No null pointers**: Use `Option<T>` instead
3. **No dangling pointers**: Borrow checker prevents
4. **No use-after-free**: Ownership system prevents
5. **No buffer overflows**: Bounds checking

---

## Level 2: Implementation Guide

### Ownership System

#### The Three Rules

1. Each value has a variable that's its owner
2. There can only be one owner at a time
3. When the owner goes out of scope, the value is dropped

```rust
fn takes_ownership(s: String) {
    println!("{}", s);
} // s dropped here

fn main() {
    let s = String::from("hello");
    takes_ownership(s); // s moved
    // println!("{}", s); // Error: value borrowed after move
}

// Return ownership
fn gives_ownership() -> String {
    String::from("hello")
}
```

### Borrowing & References

```rust
// Multiple immutable borrows allowed
fn calculate_length(s: &String) -> usize {
    s.len()
}

// Only one mutable borrow allowed
fn change(s: &mut String) {
    s.push_str(", world");
}

// Non-lexical lifetimes
let mut s = String::from("hello");
let r1 = &s;
let r2 = &s;
println!("{} and {}", r1, r2); // last use of r1 and r2
let r3 = &mut s; // OK: r1 and r2 no longer used
```

### Lifetime Annotations

```rust
// Specify reference relationships
fn longest<'a>(x: &'a str, y: &'a str) -> &'a str {
    if x.len() > y.len() { x } else { y }
}

// Struct with lifetime
struct Excerpt<'a> {
    part: &'a str,
}

// Lifetime elision: compiler infers in common patterns
fn first_word(s: &str) -> &str { /* elided lifetimes */ }
```

### Traits & Generics

```rust
pub trait Summary {
    fn summarize(&self) -> String;

    // Default implementation
    fn full_summary(&self) -> String {
        format!("Read more: {}", self.summarize())
    }
}

impl Summary for Article {
    fn summarize(&self) -> String {
        format!("{} by {}", self.headline, self.author)
    }
}

// Trait bounds
pub fn notify<T: Summary + Display>(item: &T) {
    println!("Breaking: {}", item.summarize());
}

// where clause for readability
pub fn process<T, U>(t: &T, u: &U) -> String
where
    T: Display + Clone,
    U: Clone + Debug,
{
    format!("{:?}", u)
}
```

### Common Derived Traits

```rust
#[derive(Debug, Clone, PartialEq, Eq, Hash, Default)]
pub struct User {
    pub id: u32,
    pub name: String,
}
```

### Error Handling

```rust
use thiserror::Error;

#[derive(Error, Debug)]
pub enum AppError {
    #[error("IO error: {0}")]
    Io(#[from] std::io::Error),

    #[error("Database error: {0}")]
    Database(#[from] sqlx::Error),

    #[error("Not found: {0}")]
    NotFound(String),
}

pub type Result<T> = std::result::Result<T, AppError>;

// With anyhow for context
use anyhow::Context;

fn read_config() -> anyhow::Result<Config> {
    let content = fs::read_to_string("config.toml")
        .context("Failed to read config file")?;
    Ok(toml::from_str(&content)?)
}
```

### Async/Await

```rust
use tokio;

#[tokio::main]
async fn main() -> Result<()> {
    let result = fetch_data().await?;
    process(result).await?;
    Ok(())
}

// Concurrent execution
let futures = urls.iter().map(|url| fetch_url(url));
let results = futures::future::join_all(futures).await;

// Spawn tasks
let handle = tokio::spawn(async move {
    expensive_computation().await
});
let result = handle.await?;
```

### Channels

```rust
use tokio::sync::mpsc;

let (tx, mut rx) = mpsc::channel(32);

// Producer
tokio::spawn(async move {
    tx.send(work).await.unwrap();
});

// Consumer
while let Some(result) = rx.recv().await {
    process(result).await;
}
```

### Shared State

```rust
use std::sync::Arc;
use tokio::sync::{Mutex, RwLock};

#[derive(Clone)]
struct AppState {
    counter: Arc<Mutex<u64>>,
    cache: Arc<RwLock<HashMap<String, String>>>,
}

async fn increment(state: AppState) {
    let mut counter = state.counter.lock().await;
    *counter += 1;
}
```

### Testing

```rust
#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_basic() {
        assert_eq!(2 + 2, 4);
    }

    #[test]
    #[should_panic(expected = "divide by zero")]
    fn test_panic() {
        divide(10, 0);
    }

    #[test]
    fn test_result() -> Result<()> {
        let result = parse_number("42")?;
        assert_eq!(result, 42);
        Ok(())
    }

    #[tokio::test]
    async fn test_async() {
        let result = fetch_data().await;
        assert!(result.is_ok());
    }
}
```

### Performance Patterns

```rust
// Prefer slices over owned strings
fn count_words(text: &str) -> usize {
    text.split_whitespace().count()
}

// Use Cow for conditional cloning
use std::borrow::Cow;

fn process_text(text: &str) -> Cow<str> {
    if text.contains("bad") {
        Cow::Owned(text.replace("bad", "good"))
    } else {
        Cow::Borrowed(text)
    }
}

// Reuse buffers
let mut buffer = String::with_capacity(1024);
for item in items {
    buffer.clear();
    write!(&mut buffer, "{}", item)?;
    send(&buffer)?;
}
```

### Iterators

```rust
// Transformation pipeline (zero-cost)
let sum: i32 = vec![1, 2, 3, 4, 5]
    .iter()
    .filter(|&x| x % 2 == 0)
    .map(|x| x * 2)
    .sum();

// Collect into different types
let doubled: Vec<i32> = numbers.iter().map(|x| x * 2).collect();
let set: HashSet<i32> = numbers.into_iter().collect();
```

---

## Level 3: Deep Dive Resources

For comprehensive coverage of advanced topics, see **[REFERENCE.md](./REFERENCE.md)**:

### Advanced Patterns

- **Newtype Pattern**: Type safety via wrapper types
- **Typestate Pattern**: Encode state machines in types
- **Builder Pattern**: Fluent API construction

### Unsafe Code Guidelines

- Documenting safety invariants
- Minimizing unsafe scope
- Testing with Miri

### Complete Examples

- Full async service implementation
- Property-based testing with proptest
- Mocking with mockall
- Integration test patterns

### Configuration Files

- `config/rustfmt.toml` - Formatter configuration
- `config/clippy.toml` - Linter rules
- `templates/` - Project templates

### Performance Deep Dive

- Profiling with flamegraph
- Compiler optimization flags
- Memory profiling with valgrind

### Learning Path

1. **Start**: [Rust Book](https://doc.rust-lang.org/book/)
2. **Practice**: [Rustlings](https://github.com/rust-lang/rustlings)
3. **Patterns**: [Rust Design Patterns](https://rust-unofficial.github.io/patterns/)
4. **Async**: [Tokio Tutorial](https://tokio.rs/tokio/tutorial)

### Common Pitfalls

1. Fighting the borrow checker - design with ownership in mind
2. Over-using `.clone()` - profile before optimizing
3. Ignoring Clippy warnings - they're usually right
4. Not using `?` operator - makes error handling cleaner
5. Premature unsafe - try safe abstractions first
6. String vs &str confusion - prefer &str for parameters

---

**Skill Complete**: Comprehensive Rust coding standards covering ownership, traits, error handling, concurrency, testing, and performance. See REFERENCE.md for advanced patterns and complete examples.

Related Skills

performance-testing-standards

13
from williamzujkowski/standards

Performance testing standards for load, stress, spike, and soak testing. Covers k6 and JMeter implementation, SLI/SLO definitions, CI/CD integration, and bottleneck analysis. Use when validating system behavior under load, establishing baselines, or capacity planning.

e2e-testing-standards

13
from williamzujkowski/standards

Implement robust E2E tests with Playwright or Cypress using Page Object Model, proper waits, and CI/CD integration. Covers selector strategies, flaky test prevention, and cross-browser testing patterns.

zero-trust-security

13
from williamzujkowski/standards

Zero-trust architecture operates on the principle: "Never trust, always verify." Unlike traditional perimeter-based security, zero-trust assumes breach and verifies every request regardless of origin.

typescript-coding-standards

13
from williamzujkowski/standards

TypeScript coding standards covering strict type system, advanced types, decorators, generics, and best practices for type-safe applications. Use for TypeScript projects requiring robust type safety and maintainable code.

swift-coding-standards

13
from williamzujkowski/standards

Master Swift coding standards with Apple's guidelines, protocol-oriented design, and modern concurrency patterns

shell-scripting-standards

13
from williamzujkowski/standards

Essential patterns for reliable shell scripts - portable shebangs, error handling, quoting rules, functions, testing with Bats, and ShellCheck integration.

python-coding-standards

13
from williamzujkowski/standards

Python coding standards following PEP 8, type hints, testing best practices, and modern Python patterns. Use for Python projects requiring clean, maintainable, production-ready code with comprehensive testing.

kotlin-coding-standards

13
from williamzujkowski/standards

Master Kotlin coding standards with null safety, coroutines, and idiomatic patterns. Use when developing JVM/Android applications requiring type-safe async programming.

javascript-coding-standards

13
from williamzujkowski/standards

JavaScript/ES6+ coding standards following Airbnb guidelines, modern patterns, React best practices, and comprehensive Jest testing. Use for JavaScript projects requiring clean, maintainable, production-ready code with modern tooling.

go-coding-standards

13
from williamzujkowski/standards

Go coding standards following idiomatic Go patterns, error handling, concurrency best practices, and modern Go tooling. Use for Go projects requiring clean, efficient, production-ready code with comprehensive testing.

coding-standards

13
from williamzujkowski/standards

Comprehensive coding standards and best practices for maintainable, consistent software development across multiple languages and paradigms

too-long-level1

13
from williamzujkowski/standards

A skill with excessively long Level 1 section