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★ 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
- Read this file, then the reference for every branch your change touches:
Rc,RefCell,Arc,Mutex, threads, channels,async: sharing-and-concurrency.mdunsafe, raw pointers,extern, FFI attributes: unsafe.md- writing or changing tests: testing.md
- a new trait, a generic or
dynparameter, a closure parameter, a public API, derives, a macro: api-design.md
- Write the code. When reviewing instead, hold the diff against every rule and report each deviation with its chapter.
- Run the gate,
.claude/skills/rust-idioms/book-lints.mjs, from the directory you run cargo in. In this repository that ispackages/: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--allreports every finding. It sees only what clippy compiles: add--all-targetswhen you changed tests, and a-pper 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 neverStringjust 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: takeimpl Iterator<Item = String>and move items out withnext(), rather than&[String]plus acloneper item (ch13-03). - Pass an iterator on as an iterator. Collect into a
Veconly when something needs theVec, and returnimpl 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
'aon 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
Resultwhenever 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
expectrather thanunwrap, 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 againstexpectas well wants the error propagated, or an explicit exemption with that same reason. - Propagate with
?and letFromconvert: oneimpl From<ParseIntError> for MyErrorcovers 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 withio::Error), convert at the call site, so the error says which.?does not cross betweenOptionandResult; convert withok_ororok(ch09-02). newpromises to succeed. A constructor that returnsResultgets another name: the book usesbuild, andFromStrorTryFromsay the same through a standard trait (ch12-03).- Every
Resultis handled or propagated. When only the error matters, writeif 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 whereMetersis expected. Atypeal
Установить rust-idioms в Claude Code и Claude Desktop
Зарегайся, чтобы установить скилл
Создай бесплатный аккаунт, чтобы открыть команду установки и сохранить скилл в библиотеку.
- Открой команду установки в одну строку
- Сохраняй скиллы в синхронизируемую библиотеку
- Уведомления, когда скиллы обновляются
Разрешённые инструменты
Инструменты, которые скиллу разрешено вызывать.
Без ограничений — скилл может использовать любой инструмент.
Вложенные файлы
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 запускает скрипты?
Да, скилл несёт исполняемые скрипты. Проверь исходник перед установкой.
Похожие скиллы
doc-coauthoring
Guide users through a structured workflow for co-authoring documentation. Use when user wants to write documentation, proposals, technical specs, decision…
от Anthropicacademy-guide
Stop and check this skill before finishing any reply to a question about how to use Claude or a Claude product — it recommends matching courses, tutorials…
от Anthropicinternal-comms
A set of resources to help me write all kinds of internal communications, using the formats that my company likes to use. Claude should use this skill…
от Anthropicskill-development
This skill should be used when the user wants to "create a skill", "add a skill to plugin", "write a new skill", "improve skill description", "organize skill…
от Anthropic