lambdamechanic/sk/rust-guidelines

Claude Code and agent skill by lambdamechanic/sk

Trust tier 3
  • community
Source check pending

Checking that the SKILL.md file is still in the repository.

Pragmatic Rust conventions to keep code readable, testable, and performant for this project.

Install

npx vskill@latest install lambdamechanic/sk/rust-guidelines

SKILL.md

Pragmatic Rust Guidelines

This file contains all guidelines concatenated for easy reference.


AI Guidelines

Design with AI use in Mind (M-DESIGN-FOR-AI) { #M-DESIGN-FOR-AI }

<why>To maximize the utility you get from letting agents work in your code base.</why> <version>0.1</version>

As a general rule, making APIs easier to use for humans also makes them easier to use by AI. If you follow the guidelines in this book, you should be in good shape.

Rust's strong type system is a boon for agents, as their lack of genuine understanding can often be counterbalanced by comprehensive compiler checks, which Rust provides in abundance.

With that said, there are a few guidelines which are particularly important to help make AI coding in Rust more effective:

  • Create Idiomatic Rust API Patterns. The more your APIs, whether public or internal, look and feel like the majority of

Rust code in the world, the better it is for AI. Follow the Rust API Guidelines along with the guidelines from Library / UX.

  • Provide Thorough Docs. Agents love good detailed docs. Include docs for all of your modules and public items in your crate.

Assume the reader has a solid, but not expert, level of understanding of Rust, and that the reader understands the standard library. Follow C-CRATE-DOC, C-FAILURE, C-LINK, and M-MODULE-DOCS M-CANONICAL-DOCS.

  • Provide Thorough Examples. Your documentation should have directly usable examples, the repository should include more elaborate ones.

Follow C-EXAMPLE C-QUESTION-MARK.

  • Use Strong Types. Avoid primitive obsession by using strong types with strict well-documented semantics.

Follow C-NEWTYPE.

  • Make Your APIs Testable. Design APIs which allow your customers to test their use of your API in unit tests. This might involve introducing some mocks, fakes,

or cargo features. AI agents need to be able to iterate quickly to prove that the code they are writing that calls your API is working correctly.

  • Ensure Test Coverage. Your own code should have good test coverage over observable behavior.

This enables agents to work in a mostly hands-off mode when refactoring.


Application Guidelines

Applications may use Anyhow or Derivatives (M-APP-ERROR) { #M-APP-ERROR }

<why>To simplify application-level error handling.</why> <version>0.1</version>

Note, this guideline is primarily a relaxation and clarification of [M-ERRORS-CANONICAL-STRUCTS].

Applications, and crates in your own repository exclusively used from your application, may use anyhow, eyre or similar application-level error crates instead of implementing their own types.

For example, in your application crates you may just re-export and use eyre's common Result type, which should be able to automatically handle all third party library errors, in particular the ones following [M-ERRORS-CANONICAL-STRUCTS].

rust,ignore
use eyre::Result;
fn start_application() -> Result<()> {
start_server()?;
Ok(())
}

Once you selected your application error crate you should switch all application-level errors to that type, and you should not mix multiple application-level error types.

Libraries (crates used by more than one crate) should always follow [M-ERRORS-CANONICAL-STRUCTS] instead.

[M-ERRORS-CANONICAL-STRUCTS]: ../libs/ux/#M-ERRORS-CANONICAL-STRUCTS

Use Mimalloc for Apps (M-MIMALLOC-APPS) { #M-MIMALLOC-APPS }

<why>To get significant performance for free.</why> <version>0.1</version>

Applications should set mimalloc as their global allocator. This usually results in notable performance increases along allocating hot paths; we have seen up to 25% benchmark improvements.

Changing the allocator only takes a few lines of code. Add mimalloc to your Cargo.toml like so:

toml
[dependencies]
mimalloc = { version = "0.1" } # Or later version if available

Then use it from your main.rs:

rust,ignore
use mimalloc::MiMalloc;
#[global_allocator]
static GLOBAL: MiMalloc = MiMalloc;

Documentation

Documentation Has Canonical Sections (M-CANONICAL-DOCS) { #M-CANONICAL-DOCS }

<why>To follow established and expected Rust best practices.</why> <version>1.0</version>

Public library items must contain the canonical doc sections. The summary sentence must always be present. Extended documentation and examples are strongly encouraged. The other sections must be present when applicable.

rust
/// Summary sentence < 15 words.
///
/// Extended documentation in free form.
///
/// # Examples
/// One or more examples that show API usage like so.
///
/// # Errors
/// If fn returns `Result`, list known error conditions
///
/// # Panics
/// If fn may panic, list when this may happen
///
/// # Safety
/// If fn is `unsafe` or may otherwise cause UB, this section must list
/// all conditions a caller must uphold.
///
/// # Abort
/// If fn may abort the process, list when this may happen.
pub fn foo() {}

In contrast to other languages, you should not create a table of parameters. Instead parameter use is explained in plain text. In other words, do not

rust,ignore
/// Copies a file.
///
/// # Parameters
/// - src: The source.
/// - dst: The destination.
fn copy(src: File, dst: File) {}

but instead:

rust,ignore
/// Copies a file from `src` to `dst`.
fn copy(src: File, dst: File) {}
Related Reading
  • Function docs include error, panic, and safety considerations (C-FAILURE)

Mark pub use Items with #[doc(inline)] (M-DOC-INLINE) { #M-DOC-INLINE }

<why>To make re-exported items 'fit in' with their non re-exported siblings.</why> <version>1.0</version>

When publicly re-exporting crate items via pub use foo::Foo or pub use foo::*, they show up in an opaque re-export block. In most cases, this is not helpful to the reader:

[Image: TEXT]

Instead, you should annotate them with #[doc(inline)] at the use site, for them to be inlined organically:

rust,edition2021,ignore
# pub(crate) mod foo { pub struct Foo; }
#[doc(inline)]
pub use foo::*;
// or
#[doc(inline)]
pub use foo::Foo;

[Image: TEXT]

This does not apply to std or 3rd party types; these should always be re-exported without inlining to make it clear they are external.

### <alert></alert> Still avoid glob exports
The #[doc(inline)] trick above does not change [M-NO-GLOB-REEXPORTS]; you generally should not re-export items via wildcards.

[M-NO-GLOB-REEXPORTS]: ../libs/resilience/#M-NO-GLOB-REEXPORTS

First Sentence is One Line; Approx. 15 Words (M-FIRST-DOC-SENTENCE) { #M-FIRST-DOC-SENTENCE }

<why>To make API docs easily skimmable.</why> <version>1.0</version>

When you document your item, the first sentence becomes the "summary sentence" that is extracted and shown in the module summary:

rust
/// This is the summary sentence, shown in the module summary.
///
/// This is other documentation. It is only shown in that item's detail view.
/// Sentences here can be as long as you like and it won't cause any issues.
fn some_item() { }

Since Rust API documentation is rendered with a fixed max width, there is a naturally preferred sentence length you should not exceed to keep things tidy on most screens.

If you keep things in a line, your docs will become easily skimmable. Compare, for example, the standard library:

[Image: TEXT]

Otherwise, you might end up with _widows_ and a generally unpleasant reading flow:

[Image: TEXT]

As a rule of thumb, the first sentence should not exceed 15 words.

Has Comprehensive Module Documentation (M-MODULE-DOCS) { #M-MODULE-DOCS }

<why>To allow for better API docs navigation.</why> <version>1.1</version>

Any public library module must have //! module documentation, and the first sentence must follow [M-DOC-FIRST-SENTENCE].

rust,edition2021,ignore
pub mod ffi {
//! Contains FFI abstractions.
pub struct String {};
}

The rest of the module documentation should be comprehensive, i.e., cover the most relevant technical aspects of the contained items, including

  • what the module contains
  • when it should be used, possibly when not
  • examples
  • subsystem specifications (e.g., std::fmt also describes its formatting language)
  • observable side effects, including what guarantees are made about these, if any
  • relevant implementation details, e.g., the used system APIs

Great examples include:

This does not mean every module should contain all of these items. But if there is something to say about the interaction of the contained types, their module documentation is the right place.

[M-DOC-FIRST-SENTENCE]: ./#M-DOC-FIRST-SENTENCE


FFI Guidelines

Isolate DLL State Between FFI Libraries (M-ISOLATE-DLL-STATE) { #M-ISOLATE-DLL-STATE }

<why>To prevent data corruption and undefined behavior.</why> <version>0.1</version>

When loading multiple Rust-based dynamic libraries (DLLs) within one application, you may only share 'portable' state between these libraries. Likewise, when authoring such libraries, you must only accept or provide 'portable' data from foreign DLLs.

Portable here means data that is safe and consistent to process regardless of its origin. By definition, this is a subset of FFI-safe types. A type is portable if it is #[repr(C)] (or similarly well-defined), and _all_ of the following:

  • It must not have any interaction with any static or thread local.
  • It must not have any interaction with any TypeId.
  • It must not contain any value, pointer or reference to any non-portable data (it is valid to point into portable data within non-portable data, such as

sharing a reference to an ASCII string held in a Box).

_Interaction_ means any computational relationship, and therefore also relates to how the type is used. Sending a u128 between DLLs is OK, using it to exchange a transmuted TypeId isn't.

The underlying issue stems from the Rust compiler treating each DLL as an entirely new compilation artifact, akin to a standalone application. This means each DLL:

  • has its own set of static and thread-local variables,
  • the type layout of any #[repr(Rust)] type (the default) can differ between compilations,
  • has its own set of unique type IDs, differing from any other DLL.

Notably, this affects:

  • ⚠️ any allocated instance, e.g., String, Vec<u8>, Box<Foo>, ...
  • ⚠️ any library relying on other statics, e.g., tokio, log,
  • ⚠️ any struct not #[repr(C)],
  • ⚠️ any data structure relying on consistent TypeId.

In practice, transferring any of the above between libraries leads to data loss, state corruption, and usually undefined behavior.

Take particular note that this may also apply to types and methods that are invisible at the FFI boundary:

This is the start of the file. Read the full SKILL.md on GitHub.

Security

  • Security scanPassed, 100 of 100Pattern checks on the skill's files.
  • Model reviewPassed

Quality evaluation

  • Task evaluationNot runNo evaluation yet. Run one in Skill Studio.

Source

Popularity

Repository stars
2
7-day trend score
5

Stars and forks are for the whole GitHub repository, not this skill alone. Refreshed 7 Apr 2026, 7 months ago.

Works with

Any agent vskill installs to: 56 targets in all. These 39 have a page on this site.

  • Claude Code
  • Cursor
  • GitHub Copilot
  • Windsurf
  • Codex
  • Gemini CLI
  • Cline
  • Amp
  • Roo Code
  • Goose
  • Continue
  • Kilo Code
  • Junie
  • Trae
  • OpenHands
  • Qwen Code
  • AI Agent
  • Tabnine Chat
  • Sourcegraph Cody
  • Amazon Q
  • Firebase Genkit
  • Sweep
  • Devin
  • Phind
  • Mentat
  • Claude Desktop
  • Aider
  • GPT Pilot
  • Plandex
  • bolt.new
  • v0
  • Replit Agent
  • Anse
  • TypingMind
  • BetterChatGPT
  • LibreChat
  • Warp AI
  • Pieces
  • Fig AI
Embed badge
vskill VERIFIED badge
[![vskill VERIFIED](https://verified-skill.com/api/v1/skills/lambdamechanic/sk/rust-guidelines/badge)](https://verified-skill.com/skills/lambdamechanic/sk/rust-guidelines)

Something wrong with this skill or its page? Tell us.