Preludes
Aprelude is a collection of names that are automatically brought into scopeof every module in a crate.
These prelude names are not part of the module itself: they are implicitlyqueried duringname resolution. For example, even though something likeBox
is in scope in every module, you cannot refer to it asself::Box
because it is not a member of the current module.
There are several different preludes:
Standard library prelude
Each crate has a standard library prelude, which consists of the names from a single standard library module.
The module used depends on the crate’s edition, and on whether theno_std
attribute is applied to the crate:
Edition | no_std not applied | no_std applied |
---|---|---|
2015 | std::prelude::rust_2015 | core::prelude::rust_2015 |
2018 | std::prelude::rust_2018 | core::prelude::rust_2018 |
2021 | std::prelude::rust_2021 | core::prelude::rust_2021 |
2024 | std::prelude::rust_2024 | core::prelude::rust_2024 |
Note
std::prelude::rust_2015
andstd::prelude::rust_2018
have the same contents asstd::prelude::v1
.
core::prelude::rust_2015
andcore::prelude::rust_2018
have the same contents ascore::prelude::v1
.
Extern prelude
External crates imported withextern crate
in the root module or providedto the compiler (as with the--extern
flag withrustc
) are added to theextern prelude. If imported with an alias such asextern crate orig_name as new_name
, then the symbolnew_name
is instead added to the prelude.
Thecore
crate is always added to the extern prelude.
Thestd
crate is added as long as theno_std
attribute is not specified in the crate root.
2018 Edition differences
In the 2015 edition, crates in the extern prelude cannot be referenced viause declarations, so it is generally standard practice to include
extern crate
declarations to bring them into scope.Beginning in the 2018 edition,use declarations can reference crates in the extern prelude, so it is considered unidiomatic to use
extern crate
.
Note
Additional crates that ship with
rustc
, such asalloc
, andtest
, are not automatically included with the--extern
flag when using Cargo. They must be brought into scope with anextern crate
declaration, even in the 2018 edition.#![allow(unused)]fn main() {extern crate alloc;use alloc::rc::Rc;}
Cargo does bring in
proc_macro
to the extern prelude for proc-macro crates only.
Theno_std
attribute
By default, the standard library is automatically included in the crate rootmodule. Thestd
crate is added to the root, along with an implicitmacro_use
attribute pulling in all macros exported fromstd
into themacro_use
prelude. Bothcore
andstd
are added to theexternprelude.
Theno_std
attribute may be applied at the crate level to prevent thestd
crate from being automatically added into scope.
It does three things:
- Prevents
std
from being added to theextern prelude.
- Affects which module is used to make up thestandard library prelude (as described above).
- Injects the
core
crate into the crate root instead ofstd
, and pullsin all macros exported fromcore
in themacro_use
prelude.
Note
Using the core prelude over the standard prelude is useful when either the crate is targeting a platform that does not support the standard library or is purposefully not using the capabilities of the standard library. Those capabilities are mainly dynamic memory allocation (e.g.
Box
andVec
) and file and network capabilities (e.g.std::fs
andstd::io
).
Warning
Using
no_std
does not prevent the standard library from being linked in. It is still valid to putextern crate std;
into the crate and dependencies can also link it in.
Language prelude
The language prelude includes names of types and attributes that are built-into the language. The language prelude is always in scope.
It includes the following:
- Type namespace
- Boolean type —
bool
- Textual types —
char
andstr
- Integer types —
i8
,i16
,i32
,i64
,i128
,u8
,u16
,u32
,u64
,u128
- Machine-dependent integer types —
usize
andisize
- floating-point types —
f32
andf64
- Boolean type —
- Macro namespace
macro_use
prelude
Themacro_use
prelude includes macros from external crates that wereimported by themacro_use
attribute applied to anextern crate
.
Tool prelude
The tool prelude includes tool names for external tools in thetypenamespace. See thetool attributes section for more details.
Theno_implicit_prelude
attribute
Theno_implicit_prelude
attribute may be applied at the crate level oron a module to indicate that it should not automatically bring thestandardlibrary prelude,extern prelude, ortool prelude into scope for thatmodule or any of its descendants.
This attribute does not affect thelanguage prelude.
2018 Edition differences
In the 2015 edition, the
no_implicit_prelude
attribute does not affect themacro_use
prelude, and all macros exported from the standard library are still included in themacro_use
prelude. Starting in the 2018 edition, it will remove themacro_use
prelude.