Structs & Enums Best Practices
Design data types so invariants live in one place and the compiler helps enforce them.
Busca en todas las páginas de la documentación
Design data types so invariants live in one place and the compiler helps enforce them.
bool flags or Option soup.struct UserId(u64) prevents argument swaps.Debug, Clone, Eq when semantics match. Document when not deriving.Vec fields unless truly unrestricted. Expose iterators or methods.enum Phase beats is_started + is_done.Ready vs Running { token }.#[non_exhaustive] on extensible public enums. Forward-compatible evolution.self. Illegal transitions become type or Result errors.match arms small; extract helpers. Readable transition functions.if let for single-pattern flows. Reduce noise vs one-arm match.let else for required patterns. Early return on unexpected shape.unwrap on domain enums in library code. Return Result with context.#[serde(tag = "type")] etc.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