rust-coding-skill
npx machina-cli add skill UtakataKyosui/C2Lab/rust-coder --openclawRust Coding Skill
Instructions
-
Fully understand the user request:
Determine whether the task involves designing data structures, implementing traits, writing macros, modeling domain logic, or organizing modules.
Identify key constraints such as mutability needs, ownership flow, async context, interior mutability, or concurrency boundaries. -
Plan data structures with precision:
- Choose between
struct,enum, ornewtypebased on domain needs. - Consider ownership of each field:
- Use
&strvsString, slices vs vectors,Arc<T>when sharing, orCow<'a, T>for flexible ownership.
- Use
- Model invariants explicitly using types (e.g.,
NonZeroU32,Duration, custom enums). - Prefer
enumfor state machines instead of boolean flags or loosely related fields.
- Choose between
-
Write idiomatic Rust implementations:
- Place
implblocks immediately below the struct/enum they modify. - Group related methods together: constructors, getters, mutation methods, domain logic, helpers.
- Provide clear constructors (
new,with_capacity, builders) where appropriate. - Use trait implementations (
Display,Debug,From,Into,TryFrom) to simplify conversions. - Prefer returning
Result<T, E>instead of panicking. - Keep functions short to help lifetime inference and clarity.
- Place
-
Apply rigorous documentation and code-style best practices:
- Use
///doc comments for structs, enums, fields, and methods. - Use
//!for module-level documentation when explaining design or architecture. - Include examples in docs where valuable.
- Run
cargo fmtandcargo clippy --all-targets --all-featuresto maintain consistency. - Reserve blank lines between logically separate methods and sections.
- Use
-
Use macros effectively but responsibly:
- Apply
derivemacros (Debug,Clone,Serialize,Deserialize, etc.) to reduce boilerplate. - Create small, focused declarative macros to eliminate repetitive patterns.
- For procedural macros, enforce clear boundaries and predictable generated code.
- Apply
-
Optimize build speed when relevant:
- On Linux, configure
.cargo/config.tomlto use themoldlinker when appropriate. - Use
sccacheto cache compiled artifacts during development. - Minimize unnecessary dependencies and feature flags.
- Prefer
cargo checkduring rapid iteration overcargo build. - Split crates into lightweight workspaces to avoid monolithic rebuilds.
- Use
cargo profilesettings for tuned dev/release defaults.
- On Linux, configure
-
Encourage maintainable module and project structure:
- Organize code into modules reflecting ownership and domain boundaries.
- Use
pub(crate)instead ofpubwhen possible; expose only what needs exposing. - Keep APIs small and expressive; avoid leaking internal types.
- Use meaningful file and module names aligned with functionality.
-
Provide explanations and alternatives:
For every code design, explain why a certain pattern is chosen and propose alternatives when relevant, such as:- builder pattern vs simple constructor
- enum-based state machine vs multiple booleans
- shared ownership via
Arc<T>vs message passing channels - slice-based APIs for performance vs owned collections for convenience
- deriving traits vs manual implementations for custom logic
-
Maintain clarity, safety, and idiomatic style at all times:
Prioritize predictable ownership flow, correct lifetimes, and ergonomic APIs that reflect common Rust patterns.
Source
git clone https://github.com/UtakataKyosui/C2Lab/blob/main/plugins/rust/skills/rust-coder/SKILL.mdView on GitHub Overview
Guides you in writing idiomatic, efficient Rust code with thoughtful data modeling, trait implementation, and clean module organization. It also covers macros use, documentation standards, and practical build-speed optimizations to keep projects maintainable.
How This Skill Works
It guides you to plan data structures with precise ownership and invariants, structure impl blocks logically, and implement useful traits. It then covers idiomatic patterns (docs, derives, helpers), prudent macro usage, and build-speed optimizations to keep code maintainable.
When to Use It
- Starting a new Rust project and defining core data models with clear ownership
- Implementing domain logic with trait-based conversions and method grouping
- Refactoring monolithic code into idiomatic modules with pub(crate) visibility
- Optimizing build speed during rapid iteration with cargo check and workspace splits
- Documenting APIs with doc comments and examples to aid maintainability
Quick Start
- Step 1: Analyze the task and decide between struct, enum, or newtype based on domain needs
- Step 2: Implement core types with clear constructors and derive common traits; add necessary From/Into/try_from
- Step 3: Run cargo fmt, cargo clippy, and apply build-speed optimizations (check vs build, minimal features, workspace splits)
Best Practices
- Plan data structures (struct/enum/newtype) with explicit ownership and invariants
- Place impl blocks immediately below the type and group constructors, getters, and domain logic
- Prefer Result<T, E> and avoid panics; use derives for boilerplate and add targeted custom logic as needed
- Document with doc comments and module docs; include runnable examples where valuable; run cargo fmt and clippy
- Use macros sparingly but effectively: derives, small declarative macros, and boundaries for procedural macros; optimize builds with mold, sccache, and workspace strategies
Example Use Cases
- A domain model for an ecommerce cart using a state-like enum and ownership-safe structs
- A config type that uses NonZeroU32 and TryFrom implementations for safe parsing
- A small macro that eliminates boilerplate for repetitive DTO structs
- A library with modules organized by ownership boundaries and pub(crate) visibility
- A cargo workspace setup using mold linker and sccache to reduce compile times