This style guide codifies a set of idiomatic conventions for writing Rust code in this project. The primary goal is to ensure that the codebase remains consistent, readable, and easy to maintain over time. Adhering to these standards reduces cognitive overhead and makes collaboration more effective.
All Rust module files (.rs) must follow a strict top-level item order to ensure predictability and ease of navigation. The canonical order is as follows:
- Module-level documentation (
//!): Explains the purpose and scope of the module. - Outer attributes (
#![...]): Compiler directives like#![deny(missing_docs)]. usedeclarations: External and internal imports.- Public re-exports (
pub use): Items re-exported from other modules. - Submodules (
mod): Child module declarations. - Constants (
const): Compile-time constants. - Static variables (
static): Globally allocated variables. - Types:
struct,enum, andtypealiases. - Traits: Trait definitions.
- Trait implementations and
implblocks: Implementations of traits and inherent methods. - Free-standing functions: Module-level functions.
- Tests (
#[cfg(test)]modules): Unit and integration tests for the module.
Within each category, items must be sorted to maintain a consistent structure.
use declarations are grouped in the following order, with each group sorted alphabetically:
std: Standard library imports.- External Crates: Third-party dependencies.
- Local Modules: Project-internal imports, starting with
crate::orsuper::.
Example:
use std::collections::HashMap;
use std::path::PathBuf;
use anyhow::Result;
use log::info;
use crate::core::Atom;
use super::utils::helper_function;All other top-level items—including modules, constants, traits, and functions—must be sorted alphabetically by their identifier.
Types (struct, enum, and type aliases) must be sorted as a single, interlaced group, alphabetically by their identifier.
Within any given category, public (pub) items must always be placed before private items. This rule applies before alphabetical sorting. For example, a public function alpha would come before a private function beta, but also before a public function zeta.
Example:
// Public items first, sorted alphabetically
pub const MAX_RETRIES: u32 = 3;
pub fn get_config() -> Config { /* ... */ }
// Private items next, sorted alphabetically
const DEFAULT_TIMEOUT: u64 = 10;
fn process_data() { /* ... */ }Clear and comprehensive documentation is mandatory for maintaining a high-quality codebase.
- All public items (modules, functions, types, traits, constants) must have descriptive documentation comments (
///). - Module-level documentation (
//!) is required for every module. It should provide a high-level overview of the module's responsibilities and how it fits into the larger system. - Comments should be clear, concise, and sufficient for a developer to understand the item's purpose and usage without needing to read the underlying source code.
- Use
rustfmt: This project uses an opinionatedrustfmt.tomlconfiguration to enforce a consistent code style. Runningcargo fmtwill automatically handle much of the formatting for you. However, the guidelines in this document (especially regarding item order and documentation) must still be followed manually. - Preserve Semantics: Never alter the meaning or behavior of the code purely for the sake of conforming to style.
- Preserve Comments and Attributes: When reordering items, ensure that all associated documentation, comments (
//), and attributes (#[...]) are moved along with the item they describe.