From 4c2cb78327c853e23371cd52762b2bd38afb50cd Mon Sep 17 00:00:00 2001 From: thesimplekid Date: Wed, 10 Apr 2024 13:53:53 +0100 Subject: [PATCH] chore: update CODE_STYLE Co-authored-by: Yuki Kishimoto --- CODE_STYLE.md | 57 +++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 53 insertions(+), 4 deletions(-) diff --git a/CODE_STYLE.md b/CODE_STYLE.md index 82eb1115..732f0dc2 100644 --- a/CODE_STYLE.md +++ b/CODE_STYLE.md @@ -126,10 +126,7 @@ mod y; // Finally, the internal crate modules and submodules use crate::{}; use super::{}; - -// Re-exports are treated as item definitions rather than imports, so they go -// after imports and modules. Use them sparingly. -pub use crate::x::Z; +use self::y::Y; ``` ## Import Style @@ -150,6 +147,20 @@ impl core::fmt::Display for RenameError { } ``` +When imports sub-modules: + +```rust +// GOOD +mod x; + +use self::x::Y; + +// BAD +mod x; + +use x::Y; +``` + ## If-let Avoid the `if let ... { } else { }` construct if possible, use `match` instead: @@ -185,3 +196,41 @@ match this.as_ref() { None => (), } ``` + +## Sub-modules + +Avoid the `mod x { .. }` construct if possible. Instead, crate a file `x.rs` and define it with `mod x;` + +**This applies to all sub-modules except `tests` and `benches`.** + +```rust +// GOOD +mod x; + +// BAD +mod x { + .. +} +``` + +```rust +// GOOD +#[cfg(test)] +mod tests { + .. +} + +// BAD +mod tests; +``` + +```rust +// GOOD +#[cfg(bench)] +mod benches { + .. +} + +// BAD +mod benches; +```