Rust API Guidelines
Follow the official Rust API guidelines mindset: predictable naming, clear conversions, fallible constructors, and ergonomic but hard-to-misuse types.
Busca en todas las páginas de la documentación
Follow the official Rust API guidelines mindset: predictable naming, clear conversions, fallible constructors, and ergonomic but hard-to-misuse types.
/// Parses a slug from user input.
pub fn from_str(s: &str) -> Result<Slug, ParseError> { /* ... */ }
impl TryFrom<String> for Slug { /* ... */ }
impl AsRef<str> for Slug { fn as_ref(&self) -> &str { &self.0 } }When to reach for this: Designing any pub crate API consumed by other teams or crates.io.
Naming conventions:
| Prefix | Cost | Example |
|---|---|---|
as_ | free | as_str() |
to_ | may alloc | to_string() |
into_ | consumes self | into_inner() |
pub struct Config { /* ... */ }
impl Config {
pub fn builder() -> ConfigBuilder { ConfigBuilder::default() }
pub fn validate(&self) -> Result<(), ValidationError> { /* ... */ }
}What this demonstrates:
TryFrom/from_str styleAsRefC-GETTER: getters named after field without get_ prefix unless confusionC-CTOR: constructors new, with_*, from_*C-ERROR: error types Error suffix, source() chainC-DEBUG: Debug for logs, Display for usersserde behind feature flag for librariesfn len(&self) not get_len.to_ or true consuming move.new should be infallible or return Result. Fix: try_new.| Alternative | Use When | Don't Use When |
|---|---|---|
| Builder only API | many optional fields | two-field struct |
Deref to inner | transparent wrapper | hiding invariants |
Rust API guidelines book (official) - align team checklist with it.
#[doc(inline)] for stable paths; avoid deep internal paths in public docs.
Prevent downstream impls on extension traits not meant for impl outside crate.
Breaking changes: remove/rename pub items, tighten generics, change Error semantics.
Edition 2024 async traits or async_trait crate with documented Send bounds.
iter, iter_mut, into_iter standard trio.
Display user-facing; Debug for engineers; thiserror handles both.
On Result-returning builders and futures users must not ignore.
#[deprecated(note = "use x")] with migration path before removal.
pub mod prelude { pub use ... } for ergonomic imports.
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: 16 jul 2026