Rust Patterns Best Practices
APIs that are hard to misuse encode invariants in types, use explicit errors, and reserve interior mutability for documented cases.
Busca en todas las páginas de la documentación
APIs that are hard to misuse encode invariants in types, use explicit errors, and reserve interior mutability for documented cases.
UserId, Email not raw u64/String in public API.parse_x methods everywhere.prelude module.Result, apps log context. No unwrap in library public paths.# Panics, # Errors, # Safety on public items. rustdoc sections filled.Rc<RefCell> graphs without diagram. Prefer arenas or unique ownership.Cow before clone. Clone at task boundary once.with_capacity.Arc<str>/Arc<[u8]>. Not Arc<String> churn.Arc + tokio::sync::Mutex or channels. Not RefCell across tasks.spawn_blocking. Document thread pool limits.unwrap(, .clone() in hot files flagged.Newtypes for IDs, Result errors, builder for config, Axum state Arc pattern.
Linear two-state flows with enum is enough.
unwrap OK in tests; production patterns still tested via Result paths.
Yes for typestate and interior mutability choices.
Anti-corruption layer with TryFrom at FFI/HTTP boundary.
egui may use interior mut; still minimize RefCell web.
SoA layout pattern over OOP graphs.
clap parse into newtypes; Result exit codes.
Pattern changes affecting public types are breaking.
Read Rust Patterns Basics + Common Anti-Patterns first week.
Stack versions: This page was written for Rust 1.97.0 (edition 2024), Tokio 1.x, Axum 0.8, serde 1.0, sqlx 0.8, clap 4, and Polars 0.46+.
Revisado por Chris St. John·Última actualización: 19 jul 2026