Rust System Event-Driven Best Practices
Comprehensive best practices guide for event-driven system programming in Rust. Contains 42 rules across 8 categories, prioritized by impact to guide async runtime usage, channel communication, threading, networking, and terminal handling.
When to Apply
Reference these guidelines when:
- Building async applications with Tokio or async-std
- Implementing network servers or clients
- Writing terminal user interfaces (TUIs)
- Managing concurrent tasks and shared state
- Handling Unix signals and graceful shutdown
Rule Categories by Priority
| Priority | Category | Impact | Prefix |
|----------|----------|--------|--------|
| 1 | Async Runtime Patterns | CRITICAL | async- |
| 2 | Channel Communication | CRITICAL | chan- |
| 3 | Threading & Synchronization | HIGH | sync- |
| 4 | Socket & Network I/O | HIGH | net- |
| 5 | Terminal & TTY Handling | MEDIUM-HIGH | term- |
| 6 | Signal & Process Control | MEDIUM | sig- |
| 7 | File I/O Streaming | MEDIUM | io- |
| 8 | Event Loop Architecture | LOW-MEDIUM | loop- |
Quick Reference
1. Async Runtime Patterns (CRITICAL)
async-spawn-blocking- Use spawn_blocking for CPU-bound workasync-select-biased- Use biased select for priority handlingasync-no-std-block- Avoid std blocking calls in async contextasync-cancellation-safe- Design cancellation-safe async operationsasync-task-local- Use task-local storage for request contextasync-structured-concurrency- Use JoinSet for structured concurrency
2. Channel Communication (CRITICAL)
chan-bounded-backpressure- Use bounded channels for backpressurechan-oneshot-response- Use oneshot channels for request-responsechan-broadcast-fanout- Use broadcast channels for fan-outchan-watch-state- Use watch channels for shared statechan-graceful-shutdown- Use channel closure for graceful shutdown
3. Threading & Synchronization (HIGH)
sync-arc-mutex-shared- Use Arc<Mutex> for shared mutable statesync-rwlock-read-heavy- Use RwLock for read-heavy workloadssync-atomic-counters- Use atomics for simple counters and flagssync-avoid-lock-await- Avoid holding std Mutex across awaitsync-semaphore-limit- Use Semaphore to limit concurrencysync-parking-lot- Use parking_lot for high-contention locks
4. Socket & Network I/O (HIGH)
net-split-reader-writer- Split sockets into reader and writer halvesnet-framing-codec- Use framing for message-based protocolsnet-connection-pool- Use connection pools for repeated connectionsnet-timeout-all-io- Add timeouts to all network operationsnet-tcp-nodelay- Set TCP_NODELAY for low-latency protocolsnet-graceful-disconnect- Implement graceful connection shutdown
5. Terminal & TTY Handling (MEDIUM-HIGH)
term-raw-mode-restore- Always restore terminal state on exitterm-alternate-screen- Use alternate screen for full-screen appsterm-async-event-stream- Use async event stream for terminal inputterm-buffered-output- Buffer terminal output for performanceterm-handle-resize- Handle terminal resize events
6. Signal & Process Control (MEDIUM)
sig-ctrl-c-graceful- Handle Ctrl-C for graceful shutdownsig-unix-signals- Handle Unix signals asynchronouslysig-child-reap- Reap child processes to avoid zombiessig-timeout-shutdown- Set shutdown timeout to force exit
7. File I/O Streaming (MEDIUM)
io-async-file-ops- Use async file operations in async contextio-stream-large-files- Stream large files instead of loading entirelyio-copy-bidirectional- Use copy_bidirectional for proxyingio-pipe-communication- Use pipes for process communicationio-flush-before-read- Flush writes before expecting responses
8. Event Loop Architecture (LOW-MEDIUM)
loop-actor-model- Use actor pattern for stateful componentsloop-event-types- Use typed events over dynamic dispatchloop-state-machine- Model protocol state as type-safe state machineloop-layered-architecture- Separate I/O from business logicloop-cancellation-token- Use CancellationToken for coordinated shutdown
How to Use
Read individual reference files for detailed explanations and code examples:
- Section definitions - Category structure and impact levels
- Rule template - Template for adding new rules
Reference Files
| File | Description | |------|-------------| | references/_sections.md | Category definitions and ordering | | assets/templates/_template.md | Template for new rules | | metadata.json | Version and reference information |