@@ -40,39 +40,91 @@ 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, 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
- --------------------- | --------------- | -------------
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
+ ------------------------- | --------------- | -------------
51
54
` crate ` | ` lib.rs ` | ` mod util; `
52
55
` crate::util ` | ` util.rs ` | ` mod config; `
53
56
` crate::util::config ` | ` util/config.rs ` |
54
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.
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
63
69
64
70
The directories and files used for loading external file modules can be
65
71
influenced with the ` path ` attribute.
66
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:
76
+
67
77
``` rust,ignore
78
+ #[path = "foo.rs"]
79
+ mod c;
80
+ ```
81
+
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;
102
+ }
103
+ ```
104
+
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):
112
+
113
+ ``` rust,ignore
114
+
68
115
#[path = "thread_files"]
69
116
mod thread {
70
- // Load the `local_data` module from `thread_files/tls.rs`
117
+ // Load the `local_data` module from `thread_files/tls.rs` relative to
118
+ // this source file's directory.
71
119
#[path = "tls.rs"]
72
120
mod local_data;
73
121
}
74
122
```
75
123
124
+
125
+
126
+ ## Prelude Items
127
+
76
128
Modules implicitly have some names in scope. These name are to built-in types,
77
129
macros imported with ` #[macro_use] ` on an extern crate, and by the crate's
78
130
[ prelude] . These names are all made of a single identifier. These names are not
0 commit comments