Skip to content

Commit daaa103

Browse files
Merge pull request #450 from ehuss/mod-rs
Document mod.rs changes.
2 parents 81964af + cb97754 commit daaa103

File tree

2 files changed

+74
-19
lines changed

2 files changed

+74
-19
lines changed

src/crates-and-source-files.md

+5-5
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,10 +42,10 @@ 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
46-
by the name of the crate itself. Every source file is a module, but not every
47-
module needs its own source file: [module definitions][module] can be nested
48-
within one file.
45+
source file: either by an explicit [`mod` item][module] in a referencing
46+
source file, or by the name of the crate itself. Every source file is a
47+
module, but not every module needs its own source file: [module
48+
definitions][module] can be nested within one file.
4949

5050
Each source file contains a sequence of zero or more `item` definitions, and
5151
may optionally begin with any number of [attributes]

src/items/modules.md

+69-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,34 +40,88 @@ 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+
## Module Source Filenames
44+
45+
A module without a body is loaded from an external file. When the module does
46+
not have a `path` attribute, the path to the file mirrors the logical [module
47+
path]. Ancestor module path components are directories, and the module's
48+
contents are in a file with the name of the module plus the `.rs` extension.
49+
For example, the following module structure can have this corresponding
50+
filesystem structure:
51+
52+
Module Path | Filesystem Path | File Contents
53+
------------------------- | --------------- | -------------
54+
`crate` | `lib.rs` | `mod util;`
55+
`crate::util` | `util.rs` | `mod config;`
56+
`crate::util::config` | `util/config.rs` |
57+
58+
Module filenames may also be the name of the module as a directory with the
59+
contents in a file named `mod.rs` within that directory. The above example can
60+
alternately be expressed with `crate::util`'s contents in a file named
61+
`util/mod.rs`. It is not allowed to have both `util.rs` and `util/mod.rs`.
62+
63+
> **Note**: Previous to `rustc` 1.30, using `mod.rs` files was the way to load
64+
> a module with nested children. It is encouraged to use the new naming
65+
> convention as it is more consistent, and avoids having many files named
66+
> `mod.rs` within a project.
67+
68+
### `path` Attribute
69+
70+
The directories and files used for loading external file modules can be
71+
influenced with the `path` attribute.
72+
73+
For `path` attributes on modules not inside inline module blocks, the file
74+
path is relative to the directory the source file is located. For example, the
75+
following code snippet would use the paths shown based on where it is located:
4776

4877
```rust,ignore
49-
// Load the `vec` module from `vec.rs`
50-
mod vec;
78+
#[path = "foo.rs"]
79+
mod c;
80+
```
5181

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;
82+
Source File | `c`'s File Location | `c`'s Module Path
83+
-------------- | ------------------- | ----------------------
84+
`src/a/b.rs` | `src/a/foo.rs` | `crate::a::b::c`
85+
`src/a/mod.rs` | `src/a/foo.rs` | `crate::a::c`
86+
87+
For `path` attributes inside inline module blocks, the relative location of
88+
the file path depends on the kind of source file the `path` attribute is
89+
located in. "mod-rs" source files are root modules (such as `lib.rs` or
90+
`main.rs`) and modules with files named `mod.rs`. "non-mod-rs" source files
91+
are all other module files. Paths for `path` attributes inside inline module
92+
blocks in a mod-rs file are relative to the directory of the mod-rs file
93+
including the inline module components as directories. For non-mod-rs files,
94+
it is the same except the path starts with a directory with the name of the
95+
non-mod-rs module. For example, the following code snippet would use the paths
96+
shown based on where it is located:
97+
98+
```rust,ignore
99+
mod inline {
100+
#[path = "other.rs"]
101+
mod inner;
56102
}
57103
```
58104

59-
The directories and files used for loading external file modules can be
60-
influenced with the `path` attribute.
105+
Source File | `inner`'s File Location | `inner`'s Module Path
106+
-------------- | --------------------------| ----------------------------
107+
`src/a/b.rs` | `src/a/b/inline/other.rs` | `crate::a::b::inline::inner`
108+
`src/a/mod.rs` | `src/a/inline/other.rs` | `crate::a::inline::inner`
109+
110+
An example of combining the above rules of `path` attributes on inline modules
111+
and nested modules within (applies to both mod-rs and non-mod-rs files):
61112

62113
```rust,ignore
63114
#[path = "thread_files"]
64115
mod thread {
65-
// Load the `local_data` module from `thread_files/tls.rs`
116+
// Load the `local_data` module from `thread_files/tls.rs` relative to
117+
// this source file's directory.
66118
#[path = "tls.rs"]
67119
mod local_data;
68120
}
69121
```
70122

123+
## Prelude Items
124+
71125
Modules implicitly have some names in scope. These name are to built-in types,
72126
macros imported with `#[macro_use]` on an extern crate, and by the crate's
73127
[prelude]. These names are all made of a single identifier. These names are not
@@ -94,5 +148,6 @@ The built-in attributes that have meaning on a function are [`cfg`],
94148
[IDENTIFIER]: identifiers.html
95149
[attribute]: attributes.html
96150
[items]: items.html
151+
[module path]: paths.html
97152
[prelude]: crates-and-source-files.html#preludes-and-no_std
98153
[the lint check attributes]: attributes.html#lint-check-attributes

0 commit comments

Comments
 (0)