Skip to content

Commit a9982c1

Browse files
committed
Document mod.rs changes.
1 parent 5e676a7 commit a9982c1

File tree

2 files changed

+24
-18
lines changed

2 files changed

+24
-18
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
@@ -42,7 +42,7 @@ extension `.rs`.
4242

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

src/items/modules.md

+22-16
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.
47-
48-
```rust,ignore
49-
// Load the `vec` module from `vec.rs`
50-
mod vec;
51-
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-
```
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:
48+
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` |
54+
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.
@@ -94,5 +99,6 @@ The built-in attributes that have meaning on a function are [`cfg`],
9499
[IDENTIFIER]: identifiers.html
95100
[attribute]: attributes.html
96101
[items]: items.html
102+
[module path]: paths.html
97103
[prelude]: crates-and-source-files.html#preludes-and-no_std
98104
[the lint check attributes]: attributes.html#lint-check-attributes

0 commit comments

Comments
 (0)