7
7
> _ UseTree_ :\
8
8
>   ;  ;   ;  ; ([ _ SimplePath_ ] <sup >?</sup > ` :: ` )<sup >?</sup > ` * ` \
9
9
>   ;  ; | ([ _ SimplePath_ ] <sup >?</sup > ` :: ` )<sup >?</sup > ` { ` (_ UseTree_ ( ` , ` _ UseTree_ )<sup >\* </sup > ` , ` <sup >?</sup >)<sup >?</sup > ` } ` \
10
- >   ;  ; | [ _ SimplePath_ ]   ; ( ` as ` [ IDENTIFIER] )<sup >?</sup >
10
+ >   ;  ; | [ _ SimplePath_ ]   ; ( ` as ` ( [ IDENTIFIER] | ` _ ` ) )<sup >?</sup >
11
11
12
12
A _ use declaration_ creates one or more local name bindings synonymous with
13
13
some other [ path] . Usually a ` use ` declaration is used to shorten the path
@@ -18,10 +18,6 @@ and [blocks], usually at the top.
18
18
[ modules ] : items/modules.html
19
19
[ blocks ] : expressions/block-expr.html
20
20
21
- > ** Note** : Unlike in many languages, ` use ` declarations in Rust do * not*
22
- > declare linkage dependency with external crates. Rather, [ ` extern crate `
23
- > declarations] ( items/extern-crates.html ) declare linkage dependencies.
24
-
25
21
Use declarations support a number of convenient shortcuts:
26
22
27
23
* Simultaneously binding a list of paths with a common prefix, using the
@@ -124,7 +120,7 @@ mod foo {
124
120
fn main () {}
125
121
```
126
122
127
- > ** Edition Differences** : In the 2015 Edition , ` use ` paths also allow
123
+ > ** Edition Differences** : In the 2015 edition , ` use ` paths also allow
128
124
> accessing items in the crate root. Using the example above, the following
129
125
> ` use ` paths work in 2015 but not 2018:
130
126
>
@@ -133,7 +129,13 @@ fn main() {}
133
129
> use ::foo::baz::foobaz;
134
130
> ```
135
131
>
136
- > In the 2018 Edition, if an in-scope item has the same name as an external
132
+ > The 2015 edition does not allow use declarations to reference the [extern
133
+ > prelude]. Thus [`extern crate`] declarations are still required in 2015 to
134
+ > reference an external crate in a use declaration. Beginning with the 2018
135
+ > edition, use declarations can specify an external crate dependency the same
136
+ > way `extern crate` can.
137
+ >
138
+ > In the 2018 edition, if an in-scope item has the same name as an external
137
139
> crate, then `use` of that crate name requires a leading `::` to
138
140
> unambiguously select the crate name. This is to retain compatibility with
139
141
> potential future changes. <!-- uniform_paths future-proofing -->
@@ -149,7 +151,52 @@ fn main() {}
149
151
> # fn main() {}
150
152
> ```
151
153
154
+ ## Underscore Imports
155
+
156
+ Items can be imported without binding to a name by using an underscore with
157
+ the form `use path as _`. This is particularly useful to import a trait so
158
+ that its methods may be used without importing the trait's symbol, for example
159
+ if the trait's symbol may conflict with another symbol. Another example is to
160
+ link an external crate without importing its name.
161
+
162
+ Asterisk glob imports will import items imported with `_` in their unnameable
163
+ form.
164
+
165
+ ```rust
166
+ mod foo {
167
+ pub trait Zoo {
168
+ fn zoo(&self) {}
169
+ }
170
+
171
+ impl<T> Zoo for T {}
172
+ }
173
+
174
+ use self::foo::Zoo as _;
175
+ struct Zoo; // Underscore import avoids name conflict with this item.
176
+
177
+ fn main() {
178
+ let z = Zoo;
179
+ z.zoo();
180
+ }
181
+ ```
182
+
183
+ The unique, unnameable symbols are created after macro expansion so that
184
+ macros may safely emit multiple references to ` _ ` imports. For example, the
185
+ following should not produce an error:
186
+
187
+ ``` rust
188
+ macro_rules! m {
189
+ ($ item : item ) => { $ item $ item }
190
+ }
191
+
192
+ m! (use std as _;);
193
+ // This expands to:
194
+ // use std as _;
195
+ // use std as _;
196
+ ```
152
197
153
198
[ IDENTIFIER ] : identifiers.html
154
199
[ _SimplePath_ ] : paths.html#simple-paths
200
+ [ `extern crate` ] : items/extern-crates.html
201
+ [ extern prelude ] : items/extern-crates.html#extern-prelude
155
202
[ path qualifiers ] : paths.html#path-qualifiers
0 commit comments