z-rust-core
Installation
SKILL.md
Rust core
Write idiomatic, zero-cost Rust: make ownership and lifetimes explicit, model errors in the type system, keep concurrency safe, and encode invariants at compile time.
Core rules
- Ownership: Prefer borrowing (
&T) over ownership transfer; explain move semantics for non-Copy types - Error Handling: Use
anyhowfor application code,thiserrorfor libraries; always propagate with?; never panic in library code - Async: Preserve the target service's existing executor and lifecycle. Use
tokioAPIs only in Tokio applications or when a new runtime choice is explicitly authorized —tokio::spawnpanics outside a Tokio runtime context; usetokio::select!for racing futures; wrap blocking calls withspawn_blocking - Traits: Use trait bounds with
impl Traitfor simple argument positions; usedyn Traitfor runtime polymorphism; implementFrom/Intofor conversions - Project Layout: For GUI desktop apps ([[z-rust-gtk4]]), split by concern, not by domain/service/handler layers — a workspace with one crate per feature (
core/,subscription/,ui/, ...) or a single crate with modules per concern (api/,app/,platform/,ui/, ...). See [[z-rust-web]] for HTTP-service layout. - Safety: Minimize
unsafe; document all safety invariants when unavoidable; use#[must_use]on types where ignoring the return is a bug; use#[non_exhaustive]on public enums for future compatibility - Tooling: Run
cargo fmtandcargo clippy -- -D warningsin CI.-W clippy::allenables warnings but does not fail on them. Add--nocaptureonly to an already bounded test command when debug output helps. - Testing: Unit tests in
#[cfg(test)]module in the same file; integration tests intests/; useproptest/quickcheckfor property-based tests; usemockallfor mocking traits andmockitoto stub HTTP responses in adapter/client tests.cargo testhas no timeout and spawns one thread per core — run dev suites astimeout 5m cargo test -- --test-threads=4(or via a runner target carrying the same limits); [[z-testing-strategy]] has the per-runner floor
Do not
- Turn on
clippy::pedanticreflexively — it earns its place with a concrete reason, not by default. - Mix
anyhowinto a library's public API — libraries return typedthiserrorerrors so callers can match on variants;anyhowis for application code that only needs to propagate and log.