AGENT SKILL

industrial-rust-techniques

Industrial-grade Rust engineering manual and best practices. Covers high-throughput compilation optimization (sccache, linkers, profile tuning, split-debuginfo), memory stability against OOM/RAM-exhaustion, robust async architectures (Tokio, Axum, Tower), zero-copy serialization, high-performance crates ecosystem, and production testing workflows.

License 许可
MIT
所属仓库
iberi22/skills
最近更新
2026-10-12
Stars
★ 0
Forks
0

01 前言 Front Matter

name
industrial-rust-techniques
description
Industrial-grade Rust engineering manual and best practices. Covers high-throughput compilation optimization (sccache, linkers, profile tuning, split-debuginfo), memory stability against OOM/RAM-exhaustion, robust async architectures (Tokio, Axum, Tower), zero-copy serialization, high-performance crates ecosystem, and production testing workflows.

03 SKILL.md 正文

Industrial Rust Techniques & Systems Engineering

Comprehensive guide, patterns, and operational protocols for building, compiling, optimizing, and maintaining production-grade Rust systems within large monorepos and resource-constrained environments.


1. Compilation Performance & Resource Optimization

1.1 Multi-Tier Build Profile Strategy

Avoid full debug symbol generation during local development. In large workspaces with crates like polars, candle, tokenizers, or axum, GNU ld can easily consume 2-4 GB of RAM per compilation thread if symbols are monolithic.

Configure global or project profiles (~/.cargo/config.toml or ./Cargo.toml):

[profile.dev]
opt-level = 0
debug = 1                # Line tables only (sufficient for backtraces and IDE debugging)
split-debuginfo = "unpacked" # Keeps debuginfo in separate object files, sparing RAM during linking

[profile.dev.package."*"]
opt-level = 2            # Compiles 3rd-party dependencies with optimization once, keeping tests fast

[profile.test]
opt-level = 0
debug = 1
split-debuginfo = "unpacked"

[profile.test.package."*"]
opt-level = 2

[profile.release]
opt-level = 3
lto = "thin"             # ThinLTO provides ~95% of full LTO performance at 20% of the link time and RAM
codegen-units = 16       # Parallel codegen units
panic = "abort"          # Eliminates unwind landing pads, reducing binary size by 15-30%
strip = "symbols"        # Strips debug symbols automatically in release

1.2 Shared Cache (sccache)

Never compile identical 3rd-party dependencies across multiple workspaces or projects from scratch.

  • Configuration (~/.cargo/config.toml):
    [build]
    rustc-wrapper = "sccache"
    
  • Status Inspection:
    sccache --show-stats
    
  • Caché Storage: Stored locally on fast NVMe storage (~/.cache/sccache), automatically LRU-evicted.

1.3 Storage & RAM-Disk Rules

  • Never point Cargo target directories into tmpfs / RAM-disks if the workspace compiles large integration tests or deep ML/data pipelines. Símbolos acumulados pueden superar 30 GB y disparar el kernel OOM killer o bloquear sockets de I/O.
  • Keep targets on local NVMe disk: Linux Page Cache will automatically use idle RAM as high-speed read/write buffers without locking memory from OS services or user terminals.

2. Modern Rust Crates Ecosystem (Production Standard)

Domain Recommended Crate(s) Role & Industrial Rationale
Async Runtime tokio = { version = "1.43", features = ["full"] } Standard battle-tested multi-threaded runtime.
HTTP / Web axum = "0.8", tower = "0.5", tower-http = "0.6" Ergonomic routing, composable middleware with zero-cost abstractions.
Error Handling thiserror = "2.0" (libs), anyhow = "1.0" (apps/bins) Idiomatic domain errors vs flexible application context.
Serialization serde = { version = "1.0", features = ["derive"] }, simd-json = "0.14" Fast JSON parsing utilizing CPU SIMD vectorization.
Observability tracing = "0.1", tracing-subscriber = { version = "0.3", features = ["env-filter", "json"] } Structured, contextual async event logging.
Concurrency / Primitives parking_lot = "0.12", crossbeam = "0.8", dashmap = "6.1" Faster, smaller mutexes and lock-free concurrent maps.
Memory / Arena bytes = "1.10", bumpalo = "3.16", smallvec = "1.13" Zero-copy byte slices and high-speed arena allocation.
Databases sqlx = { version = "0.8", features = ["runtime-tokio", "postgres", "sqlite"] } Async, compile-time verified SQL queries.
Validation validator = { version = "0.19", features = ["derive"] } Declarative payload validation.

3. High-Throughput Code Patterns

3.1 Zero-Copy String & Buffer Handling

Avoid cloning strings and byte vectors across async boundaries and request processing.

use bytes::Bytes;
use std::borrow::Cow;

// Prefer Borrowed/Cow or Bytes for payload forwarding
pub struct FramePayload<'a> {
    pub topic: Cow<'a, str>,
    pub data: Bytes, // Reference-counted slice, clone is O(1) pointer bump
}

3.2 Structured Error Hierarchy

Separate library domain errors from application edge errors:

use thiserror::Error;

#[derive(Error, Debug)]
pub enum EngineError {
    #[error("Database query failed: {0}")]
    Database(#[from] sqlx::Error),
    #[error("Unauthorized access to resource: {0}")]
    Unauthorized(String),
    #[error("Validation failed: {0}")]
    Validation(String),
}

3.3 Cancellation-Safe Async Loops

Always ensure background tasks and stream processors are cancellation-safe when selecting over channels:

use tokio::sync::mpsc;

pub async fn run_event_loop(mut rx: mpsc::Receiver<Event>) {
    loop {
        tokio::select! {
            biased; // Evaluates branches in order for predictable prioritization

            Some(event) = rx.recv() => {
                process_event(event).await;
            }
            _ = tokio::signal::ctrl_c() => {
                tracing::info!("Graceful shutdown triggered");
                break;
            }
        }
    }
}

4. Development Workflow & Fast Feedback Loops

  1. Syntax & Type Check First (cargo check): Never run full cargo build for simple syntax or type validation. cargo check skips codegen and linking completely, running in < 20% of the build time.
  2. Clippy in Industrial Mode:
    cargo clippy --workspace --all-targets -- -D warnings
    
  3. Bandwidth Preservation (Data Saver): In metered connections, enforce offline operation in ~/.cargo/config.toml:
    [net]
    offline = true
    git-fetch-with-cli = true
    
  4. Target Directory Cleanup Protocol: Clean individual crate targets periodically instead of nuking global caches:
    cargo clean -p <crate_name>
    

04 同仓库更多 Skills