@@ -11,22 +11,23 @@ features, but they end up *simplifying* the module system, to make it more
11
11
clear as to what is going on.
12
12
13
13
Note: During the 2018 edition preview, there are two variants of the module
14
- system under consideration, the "absolute use paths" variant and the "relative
14
+ system under consideration, the "uniform paths" variant and the "anchored use
15
15
paths" variant. Most of these changes apply to both variants; the two variant
16
16
sections call out the differences between the two. We encourage testing of the
17
- new "relative paths" variant introduced in edition preview 2. The release of
17
+ new "uniform paths" variant introduced in edition preview 2. The release of
18
18
the 2018 edition will use one of these two variants.
19
19
20
20
Here's a brief summary:
21
21
22
22
* ` extern crate ` is no longer needed
23
23
* The ` crate ` keyword refers to the current crate.
24
- * Relative paths variant: Paths work uniformly in both ` use ` declarations and
25
- in other code, both in the top-level module and in submodules, and may use
26
- either absolute paths or local names relative to the current module.
27
- * Absolute use paths variant: Paths in ` use ` declarations are always absolute
28
- and begin with a crate name (or ` crate ` ); paths in other code may use
29
- absolute paths or local names relative to the current module.
24
+ * Uniform paths variant: Paths work uniformly in both ` use ` declarations and in
25
+ other code. Paths work uniformly both in the top-level module and in
26
+ submodules. Any path may start with a crate, with ` crate ` , ` super ` , or
27
+ ` self ` , or with a local name relative to the current module.
28
+ * Anchored use paths variant: Paths in ` use ` declarations always start with a
29
+ crate name, or with ` crate ` , ` super ` , or ` self ` . Paths in code other than
30
+ ` use ` declarations may also start with names relative to the current module.
30
31
* The ` crate ` keyword also acts as a visibility modifier, equivalent to today's ` pub(crate) ` .
31
32
* A ` foo.rs ` and ` foo/ ` subdirectory may coexist; ` mod.rs ` is no longer needed
32
33
when placing submodules in a subdirectory.
@@ -82,21 +83,21 @@ The prefix `::` previously referred to either the crate root or an external
82
83
crate; it now unambiguously refers to an external crate. For instance,
83
84
` ::foo::bar ` always refers to the name ` bar ` inside the external crate ` foo ` .
84
85
85
- ### Relative paths variant
86
+ ### Uniform paths variant
86
87
87
- The relative paths variant of Rust 2018 simplifies and unifies path handling
88
+ The uniform paths variant of Rust 2018 simplifies and unifies path handling
88
89
compared to Rust 2015. In Rust 2015, paths work differently in ` use `
89
90
declarations than they do elsewhere. In particular, paths in ` use `
90
91
declarations would always start from the crate root, while paths in other code
91
92
implicitly started from the current module. Those differences didn't have any
92
93
effect in the top-level module, which meant that everything would seem
93
94
straightforward until working on a project large enough to have submodules.
94
95
95
- In the relative paths variant of Rust 2018, paths in ` use ` declarations and in
96
+ In the uniform paths variant of Rust 2018, paths in ` use ` declarations and in
96
97
other code always work the same way, both in the top-level module and in any
97
- submodule. You can either use a relative path from the current module, an
98
- absolute path from the top of the current crate ( starting with ` crate:: ` ), or
99
- an absolute path starting from an external crate name .
98
+ submodule. You can always use a relative path from the current module, a path
99
+ starting from an external crate name, or a path starting with ` crate ` , ` super ` ,
100
+ or ` self ` .
100
101
101
102
Code that looked like this:
102
103
@@ -134,7 +135,7 @@ will look exactly the same in Rust 2018, except that you can delete the `extern
134
135
crate` line:
135
136
136
137
``` rust,ignore
137
- // Rust 2018 (relative paths variant)
138
+ // Rust 2018 (uniform paths variant)
138
139
139
140
use futures::Future;
140
141
@@ -165,7 +166,7 @@ With Rust 2018, however, the same code will also work completely unmodified in
165
166
a submodule:
166
167
167
168
``` rust,ignore
168
- // Rust 2018 (relative paths variant)
169
+ // Rust 2018 (uniform paths variant)
169
170
170
171
mod submodule {
171
172
use futures::Future;
@@ -203,15 +204,10 @@ either rename one of the conflicting names or explicitly disambiguate the path.
203
204
To explicitly disambiguate a path, use ` ::name ` for an external crate name, or
204
205
` self::name ` for a local module or item.
205
206
206
- ### Absolute use paths variant
207
+ ### Anchored use paths variant
207
208
208
- In the absolute use paths variant of Rust 2018, paths in ` use ` declarations
209
- * must* begin with one of:
210
-
211
- - A crate name
212
- - ` crate ` for the current crate's root
213
- - ` self ` for the current module's root
214
- - ` super ` for the current module's parent
209
+ In the anchored use paths variant of Rust 2018, paths in ` use ` declarations
210
+ * must* begin with a crate name, ` crate ` , ` self ` , or ` super ` .
215
211
216
212
Code that looked like this:
217
213
@@ -232,7 +228,7 @@ use foo::Bar;
232
228
Now looks like this:
233
229
234
230
``` rust,ignore
235
- // Rust 2018 (absolute use paths variant)
231
+ // Rust 2018 (anchored use paths variant)
236
232
237
233
// 'futures' is the name of a crate
238
234
use futures::Future;
@@ -277,7 +273,7 @@ mod submodule {
277
273
278
274
In the ` futures ` example, the ` my_poll ` function signature is incorrect, because ` submodule `
279
275
contains no items named ` futures ` ; that is, this path is considered relative. But because
280
- ` use ` is absolute , ` use futures:: ` works even though a lone ` futures:: ` doesn't! With ` std `
276
+ ` use ` is anchored , ` use futures:: ` works even though a lone ` futures:: ` doesn't! With ` std `
281
277
it can be even more confusing, as you never wrote the ` extern crate std; ` line at all. So
282
278
why does it work in ` main ` but not in a submodule? Same thing: it's a relative path because
283
279
it's not in a ` use ` declaration. ` extern crate std; ` is inserted at the crate root, so
@@ -286,26 +282,26 @@ it's fine in `main`, but it doesn't exist in the submodule at all.
286
282
Let's look at how this change affects things:
287
283
288
284
``` rust,ignore
289
- // Rust 2018 (absolute use paths variant)
285
+ // Rust 2018 (anchored use paths variant)
290
286
291
287
// no more `extern crate futures;`
292
288
293
289
mod submodule {
294
- // 'futures' is the name of a crate, so this is absolute and works
290
+ // 'futures' is the name of a crate, so this is anchored and works
295
291
use futures::Future;
296
292
297
- // 'futures' is the name of a crate, so this is absolute and works
293
+ // 'futures' is the name of a crate, so this is anchored and works
298
294
fn my_poll() -> futures::Poll { ... }
299
295
}
300
296
301
297
fn main() {
302
- // 'std' is the name of a crate, so this is absolute and works
298
+ // 'std' is the name of a crate, so this is anchored and works
303
299
let five = std::sync::Arc::new(5);
304
300
}
305
301
306
302
mod submodule {
307
303
fn function() {
308
- // 'std' is the name of a crate, so this is absolute and works
304
+ // 'std' is the name of a crate, so this is anchored and works
309
305
let five = std::sync::Arc::new(5);
310
306
}
311
307
}
0 commit comments