77> _ UseTree_ :\
88>   ;  ;   ;  ; ([ _ SimplePath_ ] <sup >?</sup > ` :: ` )<sup >?</sup > ` * ` \
99>   ;  ; | ([ _ 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 >
1111
1212A _ use declaration_ creates one or more local name bindings synonymous with
1313some other [ path] . Usually a ` use ` declaration is used to shorten the path
@@ -18,10 +18,6 @@ and [blocks], usually at the top.
1818[ modules ] : items/modules.html
1919[ blocks ] : expressions/block-expr.html
2020
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-
2521Use declarations support a number of convenient shortcuts:
2622
2723* Simultaneously binding a list of paths with a common prefix, using the
@@ -124,7 +120,7 @@ mod foo {
124120fn main () {}
125121```
126122
127- > ** Edition Differences** : In the 2015 Edition , ` use ` paths also allow
123+ > ** Edition Differences** : In the 2015 edition , ` use ` paths also allow
128124> accessing items in the crate root. Using the example above, the following
129125> ` use ` paths work in 2015 but not 2018:
130126>
@@ -133,7 +129,13 @@ fn main() {}
133129> use ::foo::baz::foobaz;
134130> ```
135131>
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
137139> crate, then `use` of that crate name requires a leading `::` to
138140> unambiguously select the crate name. This is to retain compatibility with
139141> potential future changes. <!-- uniform_paths future-proofing -->
@@ -149,7 +151,52 @@ fn main() {}
149151> # fn main() {}
150152> ```
151153
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+ ```
152197
153198[ IDENTIFIER ] : identifiers.html
154199[ _SimplePath_ ] : paths.html#simple-paths
200+ [ `extern crate` ] : items/extern-crates.html
201+ [ extern prelude ] : items/extern-crates.html#extern-prelude
155202[ path qualifiers ] : paths.html#path-qualifiers
0 commit comments