AGENTS.md — Rust API Guidelines
Idiomatic public-API design for Rust crates, per the official Rust API Guidelines.
- Casing per RFC 430:
UpperCamelCasetypes/traits/variants,snake_casefns/modules/macros,SCREAMING_SNAKE_CASEconsts; acronyms are one word (Uuid). - Getters omit
get_(fn name(&self)); reservegetfor the obvious single accessor. - Conversions by cost:
as_*free borrow,to_*expensive,into_*consuming. - Iterators are
iter/iter_mut/into_iterreturningIter/IterMut/IntoIter. - Derive common traits eagerly:
Debug,Clone,Copy,PartialEq/Eq,PartialOrd/Ord,Hash,Default; addDisplayfor human output. - Conversions use
From/TryFrom/AsRef/AsMut; never hand-writeInto/TryInto. - Collections implement
FromIterator+Extend; gate Serde behind aserdefeature. - Keep types
Send + Syncwhere possible; generic I/O takesR: Read/W: Writeby value. - Error types implement
std::error::Error+Send + Sync + 'static. Dropnever panics or blocks; offerclose()/flush()->Resultfor fallible teardown.- Use newtypes for static distinctions; use enums/structs over bare
bool/Optionargs. - Flag sets use the
bitflagscrate, not enums; complex constructors get a builder. - Validate arguments — make invalid states unrepresentable, else return
Result. - Keep struct fields private; seal traits not meant for downstream impl.
- Use
#[non_exhaustive]on extensible enums/structs; don't duplicate derived bounds on the struct. - Document
# Errors/# Panics/# Safety; rustdoc examples use?, notunwrap. - Feature names have no placeholders (
std, notuse-std); crate names avoid-rs/-rust. - Source: https://rust-lang.github.io/api-guidelines/