Skip to content

Commit e5392f6

Browse files
committed
Document underscore imports.
1 parent f5c66e0 commit e5392f6

File tree

2 files changed

+50
-10
lines changed

2 files changed

+50
-10
lines changed

src/items/extern-crates.md

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
> **<sup>Syntax:<sup>**\
44
> _ExternCrate_ :\
5-
> &nbsp;&nbsp; `extern` `crate` [IDENTIFIER]&nbsp;(`as` [IDENTIFIER])<sup>?</sup> `;`
5+
> &nbsp;&nbsp; `extern` `crate` [IDENTIFIER]&nbsp;(`as` ( [IDENTIFIER] | `_` ) )<sup>?</sup> `;`
66
77
An _`extern crate` declaration_ specifies a dependency on an external crate.
88
The external crate is then bound into the declaring scope as the [identifier]
@@ -68,16 +68,24 @@ need for specifying `extern crate`.
6868
6969
<!--
7070
Possible upcoming changes that will change this:
71-
7271
The `extern_crate_item_prelude` unstable feature allows `extern crate` to
7372
update the extern prelude in certain situations, see
7473
https://github.com/rust-lang/rust/pull/54658
75-
7674
Unstable `--extern proc_macro` flag that would force the crate into the
7775
extern prelude.
7876
https://github.com/rust-lang/rust/pull/54116
7977
-->
8078
79+
## Underscore Imports
80+
81+
An external crate dependency can be declared without binding its name in scope
82+
by using an underscore with the form `extern crate foo as _`. This may be
83+
useful for crates that only need to be linked, but are never referenced, and
84+
will avoid being reported as unused.
85+
86+
The `#[macro_use]` attribute will work as usual and import the macro names
87+
into the macro-use prelude.
88+
8189
[IDENTIFIER]: identifiers.html
8290
[RFC 940]: https://github.com/rust-lang/rfcs/blob/master/text/0940-hyphens-considered-harmful.md
8391
[`alloc`]: https://doc.rust-lang.org/alloc/

src/items/use-declarations.md

Lines changed: 39 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
> _UseTree_ :\
88
> &nbsp;&nbsp; &nbsp;&nbsp; ([_SimplePath_]<sup>?</sup> `::`)<sup>?</sup> `*`\
99
> &nbsp;&nbsp; | ([_SimplePath_]<sup>?</sup> `::`)<sup>?</sup> `{` (_UseTree_ ( `,` _UseTree_ )<sup>\*</sup> `,`<sup>?</sup>)<sup>?</sup> `}`\
10-
> &nbsp;&nbsp; | [_SimplePath_]&nbsp;( `as` [IDENTIFIER] )<sup>?</sup>
10+
> &nbsp;&nbsp; | [_SimplePath_]&nbsp;( `as` ( [IDENTIFIER] | `_` ) )<sup>?</sup>
1111
1212
A _use declaration_ creates one or more local name bindings synonymous with
1313
some 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-
2521
Use 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 {
124120
fn 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,37 @@ 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+
```
152182
153183
[IDENTIFIER]: identifiers.html
154184
[_SimplePath_]: paths.html#simple-paths
185+
[`extern crate`]: items/extern-crates.html
186+
[extern prelude]: items/extern-crates.html#extern-prelude
155187
[path qualifiers]: paths.html#path-qualifiers

0 commit comments

Comments
 (0)