full-stack-skills/rust-skills

rust-concurrency

Design, implement, diagnose, and test Rust concurrency and parallelism with threads, Send and Sync, locks, atomics, channels, Tokio, Rayon, Crossbeam, bounded backpressure, actor ownership, task supervision, graceful shutdown, runtime diagnostics, and Loom…

Quelltext ansehen
Originales Skill-Dokument

Aus dem Quell-Repository gerendert; Überschriften, Beispiele, Code, Tabellen, Links und Bilder bleiben erhalten.

Rust Concurrency

Based on the standard library std::thread, std::sync, and std::sync::atomic modules, along with the Async Book. Use when designing, debugging, load-testing, or reviewing threaded and async Rust code; cancellation, task ownership, lock scope, runtime sizing, queues, overload management, message passing, hand basic ownership to rust-stable and unsafe invariants to rust-unsafe-ffi.

Capability Boundaries

✅ Strengths

  1. OS threads (thread::spawn, Builder, join, scoped threads, move closures)
  2. Synchronization primitives (Mutex, RwLock, Barrier, Condvar, OnceLock, LazyLock)
  3. Atomic types (AtomicBool/Isize/Usize, load/store/fetchadd/swap/compareexchange, Ordering)
  4. Channels (mpsc: multi-producer single-consumer, Receiver, Sender)
  5. Send / Sync trait system (automatic derivation and manual implementation)
  6. async/await syntax with the Future trait
  7. Tokio runtime (tokio::main, tokio::spawn, select!, JoinSet)
  8. Async I/O foundations (tokio::fs, tokio::net, tokio::io)
  9. Bounded queues, backpressure, slow consumers, concurrency limits and overload strategies
  10. Task supervision, connection lifecycles, cancellation safety and graceful shutdown
  11. CPU-bound data parallelism and dedicated Rayon pools
  12. Crossbeam channels, queues, work-stealing deques, and scoped threads
  13. Read-heavy snapshots, sharded maps, caches, and alternative locks when measurements justify them
  14. Loom model checking and Tokio runtime diagnostics

⚠️ Prerequisites

  1. Understanding Rust ownership model (rust-stable)

❌ Inapplicable Scenarios

  1. Unsafe code concurrent execution → use rust-unsafe-ffi skill
  2. Basic ownership/borrowing → use rust-stable skill

When to Use

  • "Process data with multiple threads"
  • "How to write async/await"
  • "Tokio runtime usage"
  • "Shared data between threads"
  • "Avoid data races"
  • "Rate limiting and graceful shutdown in high-concurrency services"
  • "Tokio channel backlog or slow consumers"

Data Privacy

This skill does not collect, store, or transmit any user data.


I. OS Threads

rust
use std::thread;

let handle = thread::spawn(move || {
    println!("Hello from thread!");
});
handle.join().unwrap();

// Thread with configuration
let builder = thread::Builder::new()
    .name("worker".into())
    .stack_size(1024 * 1024);
let handle = builder.spawn(move || { /* ... */ }).unwrap();

// scoped threads (1.63+)
let mut v = vec![1, 2, 3];
thread::scope(|s| {
    s.spawn(|| {
        v.push(4); // borrow, no move required
    });
});
println!("{v:?}"); // v remains usable

II. Synchronization Primitives

rust
use std::sync::{Arc, Mutex, RwLock, Barrier, OnceLock, LazyLock};

// Mutex (mutual exclusion lock)
let counter = Arc::new(Mutex::new(0));
let mut handles = vec![];

for _ in 0..10 {
    let counter = Arc::clone(&counter);
    handles.push(thread::spawn(move || {
        let mut num = counter.lock().unwrap();
        *num += 1;
    }));
}

// RwLock (read-write lock)
let data = Arc::new(RwLock::new(vec![1, 2, 3]));
{
    let read = data.read().unwrap();
    assert_eq!(read.len(), 3);
} // Drop the read guard before taking the write lock.
data.write().unwrap().push(4);

// OnceLock (thread-safe lazy initialization)
static CONFIG: OnceLock<String> = OnceLock::new();
let config = CONFIG.get_or_init(|| load_config());

// LazyLock
static CACHE: LazyLock<HashMap<String, Data>> = LazyLock::new(HashMap::new);

III. Atomic Operations

rust
use std::sync::atomic::{
    AtomicBool, AtomicU64, Ordering
};

static COUNTER: AtomicU64 = AtomicU64::new(0);
COUNTER.fetch_add(1, Ordering::SeqCst);

static READY: AtomicBool = AtomicBool::new(false);
READY.store(true, Ordering::Release);
let ready = READY.load(Ordering::Acquire);

// Ordering levels
// Relaxed — no ordering guarantees (only atomicity)
// Release — write visibility
// Acquire — read visibility
// AcqRel — both reads and writes visible
// SeqCst — global sequential order (strongest, but not automatically default; explicit Ordering required for atomic operations)

IV. Channels

rust
use std::sync::mpsc;

let (tx, rx) = mpsc::channel();
thread::spawn(move || {
    tx.send(1).unwrap();
    tx.send(2).unwrap();
});
for received in rx {
    println!("Got: {received}");
}

// Multi-producer scenario
let (tx, rx) = mpsc::channel();
let tx1 = tx.clone();

V. async/await

rust
use tokio::time;

async fn do_work(id: u32) -> &'static str {
    time::sleep(time::Duration::from_secs(1)).await;
    println!("Task {id} done");
    "ok"
}

#[tokio::main]
async fn main() {
    // Concurrent execution
    let (r1, r2) = tokio::join!(do_work(1), do_work(2));

    // select!
    tokio::select! {
        result = do_work(1) => println!("task1: {result}"),
        result = do_work(2) => println!("task2: {result}"),
    }

    // tokio::spawn
    let handle = tokio::spawn(do_work(3));
    handle.await.unwrap();
}

VI. Send / Sync

rust
// T is Send if its ownership can be transferred across threads
// &T is Sync if it can be shared references across threads

// Types that are both Send + Sync: Arc<Mutex<T>>, i32, &'static str
// !Send types: Rc<T>, *const T
// !Sync types: RefCell<T>, Cell<T>

// Manual implementations are unsafe contracts. Do not add them merely to
// satisfy a compiler error; prove aliasing, lifetime, and thread-safety first.

VII. Select the Execution Model

WorkloadDefault starting pointAvoid
Many readiness-driven network operationsTokio tasks with bounded admissionOne task or buffer per unbounded input
CPU-heavy independent itemsRayon parallel iterators or a dedicated poolRunning long CPU work on Tokio workers
Blocking filesystem, FFI, or legacy APIsBounded spawn_blocking submissions or a dedicated poolTreating Tokio's blocking queue as backpressure
Synchronous MPMC messaging or work stealingCrossbeam channels, queues, or dequesSelecting lock-free structures without measurement
Small shared state with short critical sectionsstd::sync locksHolding guards across .await or callbacks
Read-mostly immutable snapshotsArcSwap after profilingA concurrent map for every read-heavy value
Shared keyed mutable stateSharded ownership or DashMap after contention testsMulti-key operations without an atomicity design
Expiring concurrent cacheMoka with explicit capacity and eviction policyAn unbounded map called a cache

Tokio is primarily for I/O concurrency; Rayon is for CPU parallelism. Mixing them requires an explicit handoff, independent concurrency limits, and shutdown ownership. Read Concurrency Tool Selection before introducing a third-party primitive.

Workflow

  1. Classify the workload — separate readiness-driven I/O, CPU parallelism, blocking calls, synchronization, and durable messaging before selecting a runtime or primitive.
  2. Write concurrency budgets — define maximum connections, in-flight tasks, queue capacity, item size, timeouts, memory, CPU pools, and shutdown deadlines.
  3. Determine state ownership — prefer partitioned or single-writer ownership; share state only with an explicit atomicity and lock-ordering contract.
  4. Select communication semantics — choose bounded point-to-point, request/reply, latest-value, lossy broadcast, or durable replay deliberately; specify queue-full and receiver-lag behavior.
  5. Supervise execution — retain task or thread handles, propagate failure, contain panic, prevent orphan work, and define caller-cancellation behavior.
  6. Design graceful shutdown — stop admission, close producers, publish cancellation, join within a deadline, flush required state, and return unresolved failures.
  7. Measure before tuning — record throughput, p50/p95/p99 latency, queue depth, saturation, task poll time, wakeups, lock wait, CPU, allocations, and RSS.
  8. Verify the model — test overload and cancellation, use Loom for small synchronization state machines, and use tokio-console or tracing for runtime stalls. Read Concurrency Testing and Diagnostics.

Gotchas

  1. Mutex::lock() returns a MutexGuard; do not await before dropping to avoid deadlocks
  2. tokio::spawn's Future must be both Send and 'static; non-Send references will cause compilation errors
  3. Async closures capture ownership differently than regular closures — use the move keyword explicitly for transfer of state
  4. Cancelled Futures in select! branches do not execute cleanup logic directly before dropping
  5. Atomic Ordering is not relational semantics; misuse of Relaxed can lead to unexpected memory ordering issues
  6. broadcast lag is distinct from normal success paths; must choose between discarding, rebuilding snapshots, disconnecting slow consumers, or persistently replaying events
  7. maxblockingthreads limits only the number of blocking threads and does not provide backpressure for submission queues; high-cost tasks require Semaphore or bounded queues
  8. JoinSet returns results in completion order; if API requires input ordering, carry indices through to restore sequence during aggregation
  9. DashMap, parking_lot, ArcSwap, and lock-free queues change semantics as well as performance; benchmarks do not replace invariant review
  10. Loom sees only synchronization performed through Loom-aware types and can suffer state-space explosion; keep models small and deterministic
  11. Rayon work may outlive the async caller unless cancellation and pool ownership are designed explicitly

On-Demand Resources

Official References

aus demselben Repository

Weitere Skills

Alle Skills
full-stack-skills
Community

rust-api-design

Design Rust library APIs that follow the Rust API Guidelines — naming (C-CASE, C-CONV, C-GETTER), interop traits (C-COMMON-TRAITS, C-CONVERT, C-ITER, C-SERDE), predictability (C-INTUITIVE, C-CONST), flexibility (C-GENERIC, C-NEWTYPE, C-EXT), type safety (C-BOOL, C-NONZERO, C-WRAPPER, C-STR), dependability (C-PANIC, C-UNWRAP), debuggability (C-DEBUG), and future-proofing (C-SEALED, C-STRUCT-FIELD, C-NON-EXHAUSTIVE). Use when users design a public crate API, choose between generics/concrete/newtype, decide trait bounds, hide implementation, avoid breakage, or ask "what is idiomatic Rust API design"; hand semver and publish workflow to rust-semver, lint config to rust-style-clippy, and in-crate layout to rust-module-layout.

Installationen
1
GitHub Stars
5
Aktualisiert
19. Sept.
full-stack-skills
Community

rust-by-example

Show Rust patterns through short compilable examples — type conversions (From/Into/TryFrom/as/Deref), flow control (if let/while let/match/loop), functions and closures (Fn/FnMut/FnOnce, captures), modules (mod/use/pub/super/self), generics and traits (bounds/associated types/trait objects), error handling (?/Result/thiserror/anyhow), attributes (derive/cfg/inline/allow), unsafe (raw pointers/unions/ABI), procedural macros (derive/attribute/function-like), and inline asm. Use when users ask "how do I write X in Rust", need a concrete pattern with copy-pasteable code, or are migrating from Java/Python/Go/C++ and want the Rust equivalent; hand architecture decisions to rust-api-design/rust-workspace, std API selection to rust-stdlib, and async runtime to rust-concurrency.

Installationen
1
GitHub Stars
5
Aktualisiert
19. Sept.
full-stack-skills
Community

rust-cargo-build

Configure, operate, diagnose, and automate Cargo for Rust packages and workspaces. Cover manifests and targets, commands, dependency resolution and features, profiles, build scripts, configuration and environment variables, caches and build diagnostics, cross-compilation, registries, packaging, publishing, metadata, CI reproducibility, and stable-versus-nightly feature gates. Use for Cargo.toml, Cargo.lock, .cargo/config.toml, cargo build/check/run/tree/metadata/package/publish, resolver or feature problems, build output and performance, private registries, and beginner Cargo workflows. Route crate selection and supply-chain audits to rust-dependencies, workspace topology to rust-workspace, test design to rust-testing, documentation design to rust-documentation, lint policy to rust-style-clippy, and API compatibility decisions to rust-semver.

Installationen
1
GitHub Stars
5
Aktualisiert
19. Sept.
full-stack-skills
Community

rust-cli

Design, implement, test, and release production Rust command-line applications, including command contracts, subcommands, configuration precedence, stdin/stdout/stderr, exit codes, file safety, daemon IPC, terminal handling, packaging, and process-level tests. Use when users ask for a Rust CLI, command parser, clap integration, Unix-style pipelines, daemon clients, PTY/TUI behavior, shell completion, or CLI release engineering.

Installationen
1
GitHub Stars
5
Aktualisiert
19. Sept.