Command Palette

Search for a command to run...

UnylyUnyly
Назад к скиллам

rust-idioms

БесплатноЗапускает вложенные скриптыНе проверен

Rust idioms from The Rust Programming Language (2024 edition) for the places where quickly written Rust drifts from it - ownership in signatures, panic versus…

NederlandseDigitaleDienstот NederlandseDigitaleDienst★ 30 звёзд на GitHub

Об этом скилле

Rust idioms

These rules come from The Rust Programming Language as shipped with the toolchain (2024 edition). They are the ones the book argues for and that the compiler and default clippy do not enforce. A project's own lints and review rules come first where they are stricter. Each rule names its chapter (ch09-03); rustup doc --book --path prints the book's index file and the chapter files sit beside it, for when you want the full reasoning.

How to use this

  1. Read this file, then the reference for every branch your change touches:
  2. Write the code. When reviewing instead, hold the diff against every rule and report each deviation with its chapter.
  3. Run the gate, .claude/skills/rust-idioms/book-lints.mjs, from the directory you run cargo in. In this repository that is packages/: node ../.claude/skills/rust-idioms/book-lints.mjs -p <crate>. It runs clippy with the lints that correspond to these rules and reports only findings on lines you changed. --base <ref> reports everything since the branch left <ref>, and --all reports every finding. It sees only what clippy compiles: add --all-targets when you changed tests, and a -p per crate you touched.

The work is done when the gate is green, or each remaining finding has a reason you state in your report, and every new function, type and module has been held against the sections below, which cover what no lint can see.

Signatures: borrow by default

  • A parameter borrows. It takes ownership only when the function stores or consumes the value. Text comes in as &str, never &String, and never String just to read it (ch04-03, ch10-03).
  • A function that creates a value returns it owned. A function that selects part of its argument returns a slice or reference into it, not an index that can go stale (ch04-02, ch04-03).
  • Every .clone() is a decision. It is fine for a few small values copied once (ch12-03). It goes when the callee can own the value instead: take impl Iterator<Item = String> and move items out with next(), rather than &[String] plus a clone per item (ch13-03).
  • Pass an iterator on as an iterator. Collect into a Vec only when something needs the Vec, and return impl Iterator<Item = T> when the caller may stop early (ch13-03).
  • A lifetime goes only on the parameters the return value borrows from, even when the compiler suggests the same 'a on all of them. When it suggests 'static, a reference is outliving its value: fix that instead (ch10-03, ch12-04).
  • A struct owns its fields (String, not &str) unless it is deliberately a short-lived view (ch05-01).

Errors: Result by default, panic for bugs

  • Return Result whenever failure is something a caller can meet: bad input, a missing file, a parse, a limit. Panicking decides for the caller that there is nothing to recover (ch09-03).
  • Panic only on a broken contract: an unexpected state that is a bug in the calling code, where the code after it relies on that state not occurring or no type could rule it out (ch09-03).
  • Where a failure is impossible by construction, use expect rather than unwrap, with the reason phrased as what should hold: .expect("hardcoded address should be valid"). The message is the marker for whoever changes the assumption later (ch09-02, ch09-03). A project that lints against expect as well wants the error propagated, or an explicit exemption with that same reason.
  • Propagate with ? and let From convert: one impl From<ParseIntError> for MyError covers every call site (ch09-02). That fits a source error with one meaning in your error type. Where it has several (a read and a write both fail with io::Error), convert at the call site, so the error says which. ? does not cross between Option and Result; convert with ok_or or ok (ch09-02).
  • new promises to succeed. A constructor that returns Result gets another name: the book uses build, and FromStr or TryFrom say the same through a standard trait (ch12-03).
  • Every Result is handled or propagated. When only the error matters, write if let Err(e) = run() (ch02-00, ch12-03).

Types: the type carries the invariant

  • Validate once, in a type. A value with a rule (a range, a format, non-empty) gets its own type with a private field, a checking constructor and a getter. Functions then take that type and skip the check (ch09-03).
  • A newtype keeps meanings apart: struct Millimeters(u32) cannot be passed where Meters is expected. A type al
Полный SKILL.md на GitHub

Установить rust-idioms в Claude Code и Claude Desktop

Зарегайся, чтобы установить скилл

Создай бесплатный аккаунт, чтобы открыть команду установки и сохранить скилл в библиотеку.

  • Открой команду установки в одну строку
  • Сохраняй скиллы в синхронизируемую библиотеку
  • Уведомления, когда скиллы обновляются
Зарегаться бесплатноУ меня уже есть аккаунт

Разрешённые инструменты

Инструменты, которые скиллу разрешено вызывать.

Без ограничений — скилл может использовать любой инструмент.

Вложенные файлы

api-design.mdbook-lints.mjsbook-lints.test.mjssharing-and-concurrency.mdtesting.mdunsafe.md

FAQ

Что делает скилл rust-idioms?

Rust idioms from The Rust Programming Language (2024 edition) for the places where quickly written Rust drifts from it - ownership in signatures, panic versus Result, types that carry their invariants, function and module size, doc sections. Use when writing, changing or reviewing Rust code.

Как установить скилл rust-idioms?

Скопируй папку скилла в ~/.claude/skills (вкладка Claude Code выше делает это одной командой), либо поставь как плагин.

Скилл rust-idioms запускает скрипты?

Да, скилл несёт исполняемые скрипты. Проверь исходник перед установкой.

Похожие скиллы

Сравнить rust-idioms с