| name | organize-rust-modules |
| description | Guide for organizing Rust modules and imports in this project. Covers import ordering (std → external crates → internal crate), preferring short imported names over fully-qualified paths, top-down module organization (public items first, high-level abstractions before low-level details), and placing error types at the end. Use when creating new Rust files, organizing module contents, adding imports, or reviewing code structure. Triggers on "module organization", "import order", "use statement", "Rust imports", "organize module", "code structure", or "module structure". |
| metadata | {"author":"torrust","version":"1.0"} |
Organizing Rust Modules
Rule 1: Imports Always First
All use statements go at the top of the file, in three ordered groups:
use std::path::{Path, PathBuf};
use std::sync::Arc;
use anyhow::Result;
use serde::{Deserialize, Serialize};
use thiserror::Error;
use crate::domain::Environment;
use crate::shared::Clock;
use super::config::Config;
Rule 2: Prefer Short Imported Names
Always import types and use short names. Never use long inline paths.
use std::sync::Arc;
use crate::presentation::views::UserOutput;
pub struct Handler {
output: Arc<UserOutput>,
}
pub struct Handler {
output: std::sync::Arc<crate::presentation::views::UserOutput>,
}
Exception: Full paths only when disambiguating same-named types:
use crate::domain::Environment as DomainEnvironment;
fn compare(a: &DomainEnvironment, b: &crate::config::Environment) { ... }
Rule 3: Top-Down Module Organization
Order items within a module:
- Public items first — what consumers care about
- High-level abstractions before low-level details
- Main responsibilities before secondary concerns
- Error types last —
pub enum MyError { ... }
pub struct MyCommand { ... }
impl MyCommand {
pub fn new(...) -> Self { ... }
pub fn execute(...) -> Result<...> { ... }
}
fn helper_fn() { ... }
#[derive(Debug, thiserror::Error)]
pub enum MyCommandError { ... }
Quick Checklist
Reference
Full guide: docs/contributing/module-organization.md