Rust Automod is a Visual Studio Code extension that automates the management of mod.rs files in Rust projects. It eliminates the repetitive task of manually creating and maintaining module declarations, keeping your Rust project structure clean and organized.
-
In Rust, a folder without a
mod.rsfile is not automatically recognized as a module. This can lead to tedious manual work when creating new files or folders. Rust Automod automates this by: -
Automatically creating a
mod.rsfile when a new Rust file is added to a folder. -
Adding new module declarations (
modorpub mod) in themod.rsfile automatically. -
Creating
mod.rsin nested folders and updating the parent module automatically. -
Removing module declarations when a Rust file is deleted.
-
Ensuring modules are sorted alphabetically (optional) and avoiding duplicates.
-
Supporting project-specific configuration through a
.rautomodfile. -
NEW: Toggle mod.rs visibility → Hide or show all
mod.rsfiles in the VSCode Explorer with a single command, keeping your workspace cleaner.
You can place a .rautomod file in the root of your Rust project (or any folder) to customize Automod behavior. The available configuration options are:
# 'pub' for public modules (pub mod), 'private' for private modules (mod)
visibility=pub
# 'alpha' to sort module declarations alphabetically, 'none' to preserve insertion order
sort=alpha
# 'enabled' to automatically run 'cargo fmt' after a change
fmt=enabled
# Comma-separated list of conditional compilation flags
cfg=feature="my_feature",unix
# Comma-separated list of file/folder names to apply this rule to
pattern=crate,private,another......
visibility=pub
sort=alpha
-
Creates all module declarations as
pub mod. -
Sorts modules alphabetically in
mod.rs.
# Make utils and helpers private, unsorted, and don't format them automatically
visibility=private
sort=none
fmt=disabled
pattern=utils,helpers
# Make all other modules public, sorted, and run 'cargo fmt' after changes
visibility=pub
sort=alpha
fmt=enabled
-
The first block applies only to files/folders named
utilsorhelpers. -
The second block (without
pattern) is a fallback rule for all other files. -
Patterns support comma-separated lists and can match file or folder names.
For projects that require conditional compilation, you can use the cfg key. It accepts a comma-separated list of conditions. Automod will generate a mod declaration for each condition.
Example .rautomod:
# This rule applies only to files/folders named 'advanced_mod'
visibility=pub
cfg=feature="serde_support", all(unix, target_pointer_width = "64")
pattern=advanced_mod
When you create a file named advanced_mod.rs, the extension will generate the following in mod.rs:
#[cfg(feature="serde_support")]
pub mod advanced_mod;
#[cfg(all(unix, target_pointer_width = "64"))]
pub mod advanced_mod;-
Automatic
mod.rscreation: No need to manually createmod.rswhen adding a new Rust file. -
Automatic module registration: Updates parent
mod.rsand child folders automatically. -
Deletion support: Removes module declarations when files are deleted.
-
Project-specific configuration:
.rautomodallows different settings per project. -
Optional sorting: Alphabetical ordering of module declarations.
-
NEW: Conditional Compilation Support: Automatically add
#[cfg(...)]attributes to new modules via.rautomod. -
NEW: Automatic Formatting: Optionally run
cargo fmtafter every change to keep code consistent. -
IntelliSense support: Autocomplete for
.rautomodkeys (visibility,sort,pattern,cfg,fmt) and values (pub,private,alpha,none,enabled,disabled). -
Linting:
.rautomodis validated with inline errors in VSCode. -
Hide/Show
mod.rsfiles: Quickly toggle the visibility of all mod.rs files via the Command Palette (Hidden/Show Rust Modules Files).
-
Open VSCode and go to Extensions.
-
Search for Rust Automod.
-
Install and reload VSCode.
-
Create a Rust project or open an existing one in VSCode.
-
Optionally, add a
.rautomodfile at the root with your desired settings. -
Create a new Rust file inside any folder:
- Automod will create or update
mod.rs. - The new module will be added automatically.
- Automod will create or update
-
Delete a Rust file:
- Automod will remove the module declaration from
mod.rs.
- Automod will remove the module declaration from
-
Use autocomplete and linting when editing
.rautomodto ensure correct configuration.
-
Formatting Prerequisite: For
fmt=enabledto work,rustfmtmust be installed (rustup component add rustfmt) and thecargocommand must be available in your system's PATH. -
If no
.rautomodfile is found, Automod will fallback to the global VSCode settings (rustautomod.visibility,rustautomod.sort, andrustautomod.fmt), defaulting to pub, none, and disabled. -
Nested folders are supported; Automod will create
mod.rsfiles recursively as needed. -
patternrules allow per-folder or per-file customization for all settings.
Contributions, issues, and feature requests are welcome!