1
1
# Modules
2
2
3
- > ** <sup >Syntax:<sup >** \
3
+ > ** <sup >Syntax:</ sup >** \
4
4
> _ Module_ :\
5
5
>   ;  ;   ;  ; ` mod ` [ IDENTIFIER] ` ; ` \
6
6
>   ;  ; | ` mod ` [ IDENTIFIER] ` { ` \
@@ -40,34 +40,88 @@ struct, enumeration, union, type parameter or crate can't shadow the name of a
40
40
module in scope, or vice versa. Items brought into scope with ` use ` also have
41
41
this restriction.
42
42
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:
47
76
48
77
``` rust,ignore
49
- // Load the `vec` module from `vec.rs`
50
- mod vec;
78
+ #[path = "foo.rs"]
79
+ mod c;
80
+ ```
51
81
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;
56
102
}
57
103
```
58
104
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):
61
112
62
113
``` rust,ignore
63
114
#[path = "thread_files"]
64
115
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.
66
118
#[path = "tls.rs"]
67
119
mod local_data;
68
120
}
69
121
```
70
122
123
+ ## Prelude Items
124
+
71
125
Modules implicitly have some names in scope. These name are to built-in types,
72
126
macros imported with ` #[macro_use] ` on an extern crate, and by the crate's
73
127
[ 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`],
94
148
[ IDENTIFIER ] : identifiers.html
95
149
[ attribute ] : attributes.html
96
150
[ items ] : items.html
151
+ [ module path ] : paths.html
97
152
[ prelude ] : crates-and-source-files.html#preludes-and-no_std
98
153
[ the lint check attributes ] : attributes.html#lint-check-attributes
0 commit comments