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,50 @@ 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
+ 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
+ - ->
42
79
43
80
[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