55>   ;  ; ` extern ` ` crate ` [ IDENTIFIER]   ; (` as ` [ IDENTIFIER] )<sup >?</sup > ` ; `
66
77An _ ` 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
1111The external crate is resolved to a specific ` soname ` at compile time, and a
1212runtime linkage requirement to that ` soname ` is passed to the linker for
1313loading at runtime. The ` soname ` is resolved at compile time by scanning the
1414compiler's library path and matching the optional ` crateid ` provided against
1515the ` crateid ` attributes that were declared on the external crate when it was
1616compiled. 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
1919Three examples of ` extern crate ` declarations:
2020
@@ -38,6 +38,51 @@ Here is an example:
3838extern 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+ External crates provided to the compiler are added to the "extern prelude"
44+ which exposes the crate names into lexical scope of every module without the
45+ need for specifying ` 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+ The `extern_crate_item_prelude ` unstable feature allows `extern crate ` to
73+ update the extern prelude in certain situations, see
74+ https: // github.com/rust-lang/rust/pull/54658
75+
76+ Unstable `-- extern proc_macro` flag that would force the crate into the
77+ extern prelude.
78+ https: // github.com/rust-lang/rust/pull/54116
79+ - ->
4280
4381[IDENTIFIER]: identifiers. html
82+ [RFC 940]: https: // github.com/rust-lang/rfcs/blob/master/text/0940-hyphens-considered-harmful.md
83+ [`alloc`]: https: // doc.rust-lang.org/alloc/
84+ [`crate :: `]: paths. html#crate
85+ [`proc_macro`]: https: // doc.rust-lang.org/proc_macro/
86+ [`self:: `]: paths. html#self
87+ [`test`]: https: // doc.rust-lang.org/test/
88+ [use declarations]: items/ use - declarations. html
0 commit comments