11# Modules
22
3- > ** <sup >Syntax:<sup >** \
3+ > ** <sup >Syntax:</ sup >** \
44> _ Module_ :\
55>   ;  ;   ;  ; ` mod ` [ IDENTIFIER] ` ; ` \
66>   ;  ; | ` mod ` [ IDENTIFIER] ` { ` \
@@ -40,21 +40,26 @@ struct, enumeration, union, type parameter or crate can't shadow the name of a
4040module in scope, or vice versa. Items brought into scope with ` use ` also have
4141this restriction.
4242
43- A module without a body is loaded from an external file, by default with the
44- same name as the module, plus the ` .rs ` extension. When a nested submodule is
45- loaded from an external file, it is loaded from a subdirectory path that
46- mirrors the module hierarchy.
43+ A module without a body is loaded from an external file. By default, the path
44+ to the file mirrors the logical [ module path] . Ancestor path components are
45+ directories, and the module's contents are in a file with the name of the
46+ module plus the ` .rs ` extension. For example, the following module structure
47+ can have this corresponding filesystem structure:
4748
48- ``` rust,ignore
49- // Load the `vec` module from `vec.rs`
50- mod vec;
49+ Module Path | Filesystem Path | File Contents
50+ --------------------- | --------------- | -------------
51+ ` crate ` | ` lib.rs ` | ` mod util; `
52+ ` crate::util ` | ` util.rs ` | ` mod config; `
53+ ` crate::util::config ` | ` util/config.rs ` |
5154
52- mod thread {
53- // Load the `local_data` module from `thread/local_data.rs`
54- // or `thread/local_data/mod.rs`.
55- mod local_data;
56- }
57- ```
55+ > ** Note** : Module filenames may also be the name of the module as a directory
56+ > with the contents in a file named ` mod.rs ` within that directory. Previous
57+ > to Rust 1.30, this was the way to load a module with nested children. The
58+ > above example can alternately be expressed with ` crate::util ` 's contents in
59+ > a file named ` util/mod.rs ` . It is not allowed to have both ` util.rs ` and
60+ > ` util/mod.rs ` . It is encouraged to use the new naming convention as it is
61+ > more consistent, and avoids having many files named ` mod.rs ` within a
62+ > project.
5863
5964The directories and files used for loading external file modules can be
6065influenced with the ` path ` attribute.
@@ -75,3 +80,4 @@ mod thread {
7580
7681[ _Item_ ] : items.html
7782[ items ] : items.html
83+ [ module path ] : paths.html
0 commit comments