@@ -57,6 +57,8 @@ fn main() {
57
57
}
58
58
```
59
59
60
+ ## ` use ` Visibility
61
+
60
62
Like items, ` use ` declarations are private to the containing module, by
61
63
default. Also like items, a ` use ` declaration can be public, if qualified by
62
64
the ` pub ` keyword. Such a ` use ` declaration serves to _ re-export_ a name. A
@@ -82,32 +84,32 @@ mod quux {
82
84
In this example, the module ` quux ` re-exports two public names defined in
83
85
` foo ` .
84
86
85
- Also note that the paths contained in ` use ` items are relative to the crate
86
- root. So, in the previous example, the ` use ` refers to ` quux::foo::{bar, baz} ` ,
87
- and not simply to ` foo::{bar, baz} ` . This also means that top-level module
88
- declarations should be at the crate root if direct usage of the declared
89
- modules within ` use ` items is desired. It is also possible to use ` self ` and
90
- ` super ` at the beginning of a ` use ` item to refer to the current and direct
91
- parent modules respectively. All rules regarding accessing declared modules in
92
- ` use ` declarations apply to both module declarations and ` extern crate `
93
- declarations.
87
+ ## ` use ` Paths
88
+
89
+ Paths in ` use ` items must start with a crate name or one of the [ path
90
+ qualifiers] ` crate ` , ` self ` , ` super ` , or ` :: ` . ` crate ` refers to the current
91
+ crate. ` self ` refers to the current module. ` super ` refers to the parent
92
+ module. ` :: ` can be used to explicitly refer to a crate, requiring an extern
93
+ crate name to follow.
94
94
95
95
An example of what will and will not work for ` use ` items:
96
+ <!-- Note: This example works as-is in either 2015 or 2018. -->
96
97
97
98
``` rust
98
99
# #![allow(unused_imports)]
99
- use foo :: baz :: foobaz; // good: foo is at the root of the crate
100
+ use std :: path :: {self , Path , PathBuf }; // good: std is a crate name
101
+ use crate :: foo :: baz :: foobaz; // good: foo is at the root of the crate
100
102
101
103
mod foo {
102
104
103
105
mod example {
104
106
pub mod iter {}
105
107
}
106
108
107
- use foo :: example :: iter; // good: foo is at crate root
108
- // use example::iter; // bad: example is not at the crate root
109
+ use crate :: foo :: example :: iter; // good: foo is at crate root
110
+ // use example::iter; // bad: relative paths are not allowed without `self`
109
111
use self :: baz :: foobaz; // good: self refers to module 'foo'
110
- use foo :: bar :: foobar; // good: foo is at crate root
112
+ use crate :: foo :: bar :: foobar; // good: foo is at crate root
111
113
112
114
pub mod bar {
113
115
pub fn foobar () { }
@@ -122,6 +124,33 @@ mod foo {
122
124
fn main () {}
123
125
```
124
126
127
+ > ** Edition Differences** : In the 2015 Edition, ` use ` paths also allow
128
+ > accessing items in the crate root. Using the example above, the following
129
+ > ` use ` paths work in 2015 but not 2018:
130
+ >
131
+ > ``` rust,ignore
132
+ > use foo::example::iter;
133
+ > use ::foo::baz::foobaz;
134
+ > ```
135
+ >
136
+ > In the 2018 Edition, if an in-scope item has the same name as an external
137
+ > crate, then `use` of that crate name requires a leading `::` to
138
+ > unambiguously select the crate name. This is to retain compatibility with
139
+ > potential future changes. <!-- uniform_paths future-proofing -->
140
+ >
141
+ > ```rust,edition2018
142
+ > // use std::fs; // Error, this is ambiguous.
143
+ > use ::std::fs; // Imports from the `std` crate, not the module below.
144
+ > use self::std::fs as self_fs; // Imports the module below.
145
+ >
146
+ > mod std {
147
+ > pub mod fs {}
148
+ > }
149
+ > # fn main() {}
150
+ > ```
151
+
152
+
125
153
[IDENTIFIER]: identifiers.html
126
154
[_SimplePath_]: paths.html#simple-paths
127
155
[_Visibility_]: visibility-and-privacy.html
156
+ [path qualifiers]: paths.html#path-qualifiers
0 commit comments