@@ -40,9 +40,17 @@ extern crate hello_world; // hyphen replaced with an underscore
40
40
41
41
## Extern Prelude
42
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 ` .
43
+ External crates imported with ` extern crate ` in the root module or provided to
44
+ the compiler (as with the ` --extern ` flag with ` rustc ` ) are added to the
45
+ "extern prelude". Crates in the extern prelude are in scope in the entire
46
+ crate, including inner modules. If imported with `extern crate orig_name as
47
+ new_name` , then the symbol ` new_name` is instead added to the prelude.
48
+
49
+ The ` core ` crate is always added to the extern prelude. The ` std ` crate
50
+ is added as long as the [ ` no_std ` ] attribute is not specified in the crate root.
51
+
52
+ The [ ` no_implicit_prelude ` ] attribute can be used on a module to disable
53
+ prelude lookups within that module.
46
54
47
55
> ** Edition Differences** : In the 2015 edition, crates in the extern prelude
48
56
> cannot be referenced via [ use declarations] , so it is generally standard
@@ -52,28 +60,20 @@ need for specifying `extern crate`.
52
60
> the extern prelude, so it is considered unidiomatic to use ` extern crate ` .
53
61
54
62
> ** 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).
63
+ > [ ` alloc ` ] , and [ ` test ` ] , are not automatically included with the ` --extern `
64
+ > flag when using Cargo. They must be brought into scope with an `extern
65
+ > crate` declaration, even in the 2018 edition.
59
66
>
60
67
> ``` rust
61
68
> 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 ;
69
+ > use proc_macro :: TokenStream ;
67
70
> ```
68
71
69
72
<! --
70
- Possible upcoming changes that will change this :
71
- The `extern_crate_item_prelude ` unstable feature allows `extern crate ` to
72
- update the extern prelude in certain situations, see
73
- https: // github.com/rust-lang/rust/pull/54658
74
- Unstable `-- extern proc_macro` flag that would force the crate into the
75
- extern prelude.
76
- https: // github.com/rust-lang/rust/pull/54116
73
+ The proc_macro / alloc / test limitation may be lifted if the `-- extern `
74
+ flag is stabilized and used . See tracking issue
75
+ https : // github.com/rust-lang/rust/issues/57288 and the unstable
76
+ `-- extern ` flag added in https : // github.com/rust-lang/rust/pull/54116.
77
77
- ->
78
78
79
79
## Underscore Imports
@@ -91,6 +91,8 @@ into the macro-use prelude.
91
91
[`#[macro_use]` attribute]: attributes. html#macro - related- attributes
92
92
[`alloc`]: https: // doc.rust-lang.org/alloc/
93
93
[`crate :: `]: paths. html#crate
94
+ [`no_implicit_prelude`]: items/ modules. html#prelude- items
95
+ [`no_std`]: crates- and- source- files. html#preludes- and- no_std
94
96
[`proc_macro`]: https: // doc.rust-lang.org/proc_macro/
95
97
[`self:: `]: paths. html#self
96
98
[`test`]: https: // doc.rust-lang.org/test/
0 commit comments