liuchengxu/dotfiles
Overview
This skill codifies Rust and blockchain development conventions to produce safe, readable, and maintainable code. It emphasizes inline string interpolation, consolidated imports, checked arithmetic, explicit error handling, documented unsafe blocks, and consistent formatting. Use it when writing Rust code or implementing blockchain features to reduce bugs and improve code review velocity.
How this skill works
The skill inspects Rust files and Cargo.toml to enforce style and safety patterns, suggesting replacements for anti-patterns such as positional formatting, in-function imports, unchecked arithmetic, and blind unwraps. It guides pre-commit checks (rustfmt, clippy, tests), enforces documentation for public APIs and SAFETY comments for unsafe blocks, and recommends dependency ordering. When activated, it points to concrete fixes and example snippets to apply best practices.
When to use it
- Editing any .rs file or Cargo.toml in a Rust project
- Implementing blockchain primitives: consensus, transactions, state machines
- Fixing compiler errors related to lifetimes, borrow checker, or traits
- Adding or reorganizing dependencies in Cargo.toml
- Preparing changes for commit: formatting, lints, and tests
Best practices
- Prefer inline interpolation: format!("{name}") instead of positional arguments
- Consolidate all use statements at the top of the file and alphabetize imports
- Use checked arithmetic (checked_add/checked_sub) and propagate overflow errors
- Avoid unwrap(); use expect with justification or return Result<T, E>
- Document every pub item and add // SAFETY: comments for all unsafe blocks
- Run cargo +nightly fmt, cargo clippy -- -D warnings, and cargo test before commit
Example use cases
- Refactoring legacy Rust code to remove unwraps and add proper error propagation
- Implementing a transaction state machine with validated state transitions and tests
- Adding a new dependency: update Cargo.toml with alphabetized entries and run validation scripts
- Reviewing unsafe memory access: require SAFETY invariants and add unit tests
- Hardening consensus code: enforce determinism, benchmark critical paths, and document economic assumptions
FAQ
Profile the code first. For performance-critical hotspots, use explicit checks and document why unchecked ops are safe, but prefer checked APIs by default.
When is unwrap acceptable?
Only in cases with provable invariants (e.g., test helpers or code immediately after a validated construction). Prefer expect with a clear message that documents the invariant.