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