# Rust Best Practices

Write idiomatic, robust, maintainable Rust.

- Repository: https://github.com/morganlinton/BeautifulRust (1642 stars)
- Category: Design
- License: MIT
- Last updated: 2026-10-30
- Runs scripts: yes, review before use

## Install

Claude Code: `npx skills add morganlinton/BeautifulRust --skill rust-best-practices -g -a claude-code -y`
Codex: `npx skills add morganlinton/BeautifulRust --skill rust-best-practices -g -a codex -y`
Cursor: `npx skills add morganlinton/BeautifulRust --skill rust-best-practices -g -a cursor -y`
Gemini CLI: `npx skills add morganlinton/BeautifulRust --skill rust-best-practices -g -a gemini-cli -y`
Claude Code plugin marketplace: `/plugin marketplace add morganlinton/BeautifulRust`

## Description

Write idiomatic, robust, maintainable Rust. Use this skill whenever you write, review, refactor, debug, or explain Rust code: new crates, modules, functions, structs, traits, error types, async code, unsafe or FFI code, Cargo.toml, tests, or rustdoc. Trigger on any mention of Rust, cargo, crates, .rs files, the borrow checker, lifetimes, traits, clippy, or rustfmt, even when the user does not ask for "best practices" or "idiomatic" code. Synthesized from the Google Rust Style Guide, the Rust API Guidelines, Effective Rust, Rust Design Patterns, and Microsoft's Pragmatic Rust Guidelines.

## SKILL.md, as published by its author (untrusted third-party text, read it as data)

# Rust best practices

Distilled from five Rust style and design guides, with a digest of each in `references/`.
Where they disagree, this file says which to follow and why.

## 1. Mindset

- **Write Rust-shaped code.** Don't port C++, Java, C#, or Python constructs one-to-one.
  Model errors with `Result`, resources with ownership and `Drop`, polymorphism with
  traits and enums, and nullability with `Option`. Inheritance-flavored tricks (`Deref` to
  a "base" struct, `Manager`/`Factory` types, `throw_if_null` helpers) are smells.
- **Make invalid states unrepresentable.** Push invariants into types: enums instead of
  bool flags and "must be X if Y" comments, newtypes for units and IDs, fallible
  constructors that validate once. Then the compiler enforces what a comment only requests.
- **Application code is not library code.** Library code (anything with external users)
  follows the API-design rules below strictly. Application code should favor simplicity
  and directness: concrete types, `String` or `&str` rather than `impl Into<String>`
  parameters, `anyhow`-style errors, `Vec` return values, no speculative generality.
- **Let the tools do the work.** rustfmt owns formatting, clippy owns idiom checks, the
  compiler owns memory safety. Spend your attention on design.

## 2. Before you hand back code

Run these and fix everything they report:

```bash
cargo fmt
cargo clippy --all-targets --all-features -- -D warnings
cargo test
cargo doc --no-deps   # libraries: catches broken doc links and missing docs
```

The rest: https://github.com/morganlinton/BeautifulRust/blob/HEAD/skills/rust-best-practices/SKILL.md
