Skip to content

Commit 768aa8d

Browse files
committed
extern crate changes.
Stabilization PR: rust-lang/rust#54403
1 parent 8d7463f commit 768aa8d

File tree

2 files changed

+50
-5
lines changed

2 files changed

+50
-5
lines changed

src/crates-and-source-files.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ fn main() {
8787

8888
All crates have a *prelude* that automatically inserts names from a specific
8989
module, the *prelude module*, into scope of each [module] and an [`extern
90-
crate]` into the crate root module. By default, the *standard prelude* is used.
90+
crate`] into the crate root module. By default, the *standard prelude* is used.
9191
The linked crate is [`std`] and the prelude module is [`std::prelude::v1`].
9292

9393
The prelude can be changed to the *core prelude* by using the `no_std`
@@ -140,6 +140,7 @@ type must be one of the following:
140140
[`Termination`]: ../std/process/trait.Termination.html
141141
[`core`]: ../core/index.html
142142
[`core::prelude::v1`]: ../core/preludce.index.html
143+
[`extern crate`]: items/extern-crates.html
143144
[`std`]: ../std/index.html
144145
[`std::prelude::v1`]: ../std/prelude/index.html
145146
[`use` declaration]: items/use-declarations.html

src/items/extern-crates.md

+48-4
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,16 @@
55
> &nbsp;&nbsp; `extern` `crate` [IDENTIFIER]&nbsp;(`as` [IDENTIFIER])<sup>?</sup> `;`
66
77
An _`extern crate` declaration_ specifies a dependency on an external crate.
8-
The external crate is then bound into the declaring scope as the `ident`
9-
provided in the `extern_crate_decl`.
8+
The external crate is then bound into the declaring scope as the [identifier]
9+
provided in the `extern crate` declaration.
1010

1111
The external crate is resolved to a specific `soname` at compile time, and a
1212
runtime linkage requirement to that `soname` is passed to the linker for
1313
loading at runtime. The `soname` is resolved at compile time by scanning the
1414
compiler's library path and matching the optional `crateid` provided against
1515
the `crateid` attributes that were declared on the external crate when it was
1616
compiled. If no `crateid` is provided, a default `name` attribute is assumed,
17-
equal to the `ident` given in the `extern_crate_decl`.
17+
equal to the [identifier] given in the `extern crate` declaration.
1818

1919
Three examples of `extern crate` declarations:
2020

@@ -38,6 +38,50 @@ Here is an example:
3838
extern crate hello_world; // hyphen replaced with an underscore
3939
```
4040

41-
[RFC 940]: https://github.com/rust-lang/rfcs/blob/master/text/0940-hyphens-considered-harmful.md
41+
## Extern Prelude
42+
43+
All external crates are available in the "extern prelude" which exposes the
44+
crate names into lexical scope of every module without the need for specifying
45+
`extern crate`.
46+
47+
> **Edition Differences**: In the 2015 edition, crates in the extern prelude
48+
> cannot be referenced via [use declarations], so it is generally standard
49+
> practice to include `extern crate` declarations to bring them into scope.
50+
>
51+
> Beginning in the 2018 edition, [use declarations] can reference crates in
52+
> the extern prelude, so it is considered unidiomatic to use `extern crate`.
53+
54+
> **Note**: Additional crates that ship with `rustc`, such as [`proc_macro`],
55+
> [`alloc`], and [`test`], currently aren't available in the extern prelude
56+
> and must be brought into scope with an `extern crate` declaration, even in
57+
> the 2018 edition. `use` paths must reference the `extern crate` item (such
58+
> as using [`crate::`] or [`self::`] path prefixes).
59+
>
60+
> ```rust
61+
> extern crate proc_macro;
62+
> // Cannot reference `proc_macro` directly because it is not in the extern prelude.
63+
> // use proc_macro::TokenStream;
64+
> // Instead, you must reference the item in scope from the `extern crate`
65+
> // declaration.
66+
> use self::proc_macro::TokenStream;
67+
> ```
68+
69+
<!--
70+
Possible upcoming changes that will change this:
71+
72+
`extern crate` items will automatically be added to the extern prelude.
73+
https://github.com/rust-lang/rust/pull/54658
74+
75+
Unstable `--extern proc_macro` flag that would force the crate into the
76+
extern prelude.
77+
https://github.com/rust-lang/rust/pull/54116
78+
-->
4279
4380
[IDENTIFIER]: identifiers.html
81+
[RFC 940]: https://github.com/rust-lang/rfcs/blob/master/text/0940-hyphens-considered-harmful.md
82+
[`alloc`]: https://doc.rust-lang.org/alloc/
83+
[`crate::`]: paths.html#crate
84+
[`proc_macro`]: https://doc.rust-lang.org/proc_macro/
85+
[`self::`]: paths.html#self
86+
[`test`]: https://doc.rust-lang.org/test/
87+
[use declarations]: items/use-declarations.html

0 commit comments

Comments
 (0)