5
5
>   ;  ; ` extern ` ` crate ` [ IDENTIFIER]   ; (` as ` [ IDENTIFIER] )<sup >?</sup > ` ; `
6
6
7
7
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 .
10
10
11
11
The external crate is resolved to a specific ` soname ` at compile time, and a
12
12
runtime linkage requirement to that ` soname ` is passed to the linker for
13
13
loading at runtime. The ` soname ` is resolved at compile time by scanning the
14
14
compiler's library path and matching the optional ` crateid ` provided against
15
15
the ` crateid ` attributes that were declared on the external crate when it was
16
16
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 .
18
18
19
19
Three examples of ` extern crate ` declarations:
20
20
@@ -38,6 +38,51 @@ Here is an example:
38
38
extern crate hello_world; // hyphen replaced with an underscore
39
39
```
40
40
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
+ - ->
42
80
43
81
[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