All skills
lukemurraynz avatar

/rust-patterns

@2cc2455

USE FOR: Rust implementation recipes for new Rust 2024 services, libraries, CLIs, and workspace scaffolds - Cargo setup, error types, async patterns, serde/config, gRPC/Protobuf, HTTP/axum, OpenTelemetry/tracing, testing, quality gates, API design, production readiness, and security/supply-chain checks. Pairs with `rust.instructions.md` and `rust-tests.instructions.md`. Load this skill when Copilot needs working Rust code patterns or configuration guidance.

Use this Skill: https://skilld.dev/gh/lukemurraynz/hve-agent-skills/rust-patterns

This session only. Nothing lands on disk.

recipesapi-design.md

≈696 tokens on demand. Your agent reads this file only when SKILL.md points to it.

Public API Design Patterns

Use this recipe when creating Rust libraries, SDK-like crates, reusable modules, or public APIs.

Constructor and Validation

#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct UserId(String);

impl UserId {
    /// Creates a user identifier.
    ///
    /// # Errors
    ///
    /// Returns an error when the identifier is blank.
    pub fn new(value: impl Into<String>) -> Result<Self> {
        let value = value.into();

        if value.trim().is_empty() {
            return Err(ServiceError::invalid_input("user id cannot be blank"));
        }

        Ok(Self(value))
    }

    pub fn as_str(&self) -> &str {
        &self.0
    }
}

Builder Pattern

#[derive(Debug, Clone)]
pub struct ClientOptions {
    pub endpoint: String,
    pub timeout: std::time::Duration,
}

#[derive(Debug, Default)]
pub struct ClientOptionsBuilder {
    endpoint: Option<String>,
    timeout: Option<std::time::Duration>,
}

impl ClientOptionsBuilder {
    pub fn endpoint(mut self, endpoint: impl Into<String>) -> Self {
        self.endpoint = Some(endpoint.into());
        self
    }

    pub fn timeout(mut self, timeout: std::time::Duration) -> Self {
        self.timeout = Some(timeout);
        self
    }

    pub fn build(self) -> Result<ClientOptions> {
        Ok(ClientOptions {
            endpoint: self
                .endpoint
                .ok_or_else(|| ServiceError::invalid_input("endpoint is required"))?,
            timeout: self.timeout.unwrap_or_else(|| std::time::Duration::from_secs(30)),
        })
    }
}

API Rules

  • Implement common traits where sensible: Debug, Clone, Default, PartialEq, Eq, Hash, Serialize, Deserialize.
  • Use From, TryFrom, AsRef, and AsMut instead of ad-hoc conversion names.
  • Prefer newtypes for identifiers, flags, and constrained strings.
  • Avoid boolean parameters when a small enum communicates intent better.
  • Keep struct fields private for public library types unless direct field access is part of the contract.
  • Use #[non_exhaustive] for public enums and structs that may grow.
  • Make traits object-safe when downstream users are likely to need trait objects.
  • Document errors, panics, and safety invariants.
  • Avoid exposing dependency-specific types unless the dependency is intentionally part of the public API.

Panic Documentation

/// Returns the configured shard.
///
/// # Panics
///
/// Panics when `index >= self.shard_count()`. Prefer `get_shard` when handling user input.
pub fn shard(&self, index: usize) -> &Shard {
    &self.shards[index]
}

Prefer fallible alternatives for user-controlled input:

pub fn get_shard(&self, index: usize) -> Option<&Shard> {
    self.shards.get(index)
}

Source: SKILL.md on GitHub

No alerts8d3 checks · Risk SAFE
  • Gen Agent Trust Hub8d

    The skill provides a comprehensive library of Rust implementation recipes and configuration baselines, emphasizing modern standards (Rust 2024) and security best practices such as secret redaction, dependency hygiene, and structured observability.

  • Socket8d

    No alerts

  • Snyk8d

    Risk: LOW · No issues

Signed by skilld at 2cc2455. This ties the file your Agent reads to that commit on GitHub. It does not review the instructions.

Last checked against GitHub last month.

Steadyupdated last month
Other metadata
metadata
{
  "last_verified": "2026-08-26",
  "layer": "scoped",
  "owner": "engineering"
}

README badge

README badge for lukemurraynz/hve-agent-skills/rust-patterns