Skip to content

Commit 166c9ce

Browse files
committed
Document mod.rs changes.
1 parent 2331d91 commit 166c9ce

File tree

2 files changed

+22
-16
lines changed

2 files changed

+22
-16
lines changed

src/crates-and-source-files.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
> Note: Although Rust, like any other language, can be implemented by an
1616
> interpreter as well as a compiler, the only existing implementation is a
17-
> compiler,and the language has always been designed to be compiled. For these
17+
> compiler, and the language has always been designed to be compiled. For these
1818
> reasons, this section assumes a compiler.
1919
2020
Rust's semantics obey a *phase distinction* between compile-time and
@@ -43,7 +43,7 @@ extension `.rs`.
4343

4444
A Rust source file describes a module, the name and location of which —
4545
in the module tree of the current crate — are defined from outside the
46-
source file: either by an explicit `mod_item` in a referencing source file, or
46+
source file: either by an explicit [`mod` item][module] in a referencing source file, or
4747
by the name of the crate itself. Every source file is a module, but not every
4848
module needs its own source file: [module definitions][module] can be nested
4949
within one file.

src/items/modules.md

+20-14
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Modules
22

3-
> **<sup>Syntax:<sup>**\
3+
> **<sup>Syntax:</sup>**\
44
> _Module_ :\
55
> &nbsp;&nbsp; &nbsp;&nbsp; `mod` [IDENTIFIER] `;`\
66
> &nbsp;&nbsp; | `mod` [IDENTIFIER] `{`\
@@ -40,21 +40,26 @@ struct, enumeration, union, type parameter or crate can't shadow the name of a
4040
module in scope, or vice versa. Items brought into scope with `use` also have
4141
this 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
5964
The directories and files used for loading external file modules can be
6065
influenced 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

Comments
 (0)