1 | //! # The Rust Prelude |
2 | //! |
3 | //! Rust comes with a variety of things in its standard library. However, if |
4 | //! you had to manually import every single thing that you used, it would be |
5 | //! very verbose. But importing a lot of things that a program never uses isn't |
6 | //! good either. A balance needs to be struck. |
7 | //! |
8 | //! The *prelude* is the list of things that Rust automatically imports into |
9 | //! every Rust program. It's kept as small as possible, and is focused on |
10 | //! things, particularly traits, which are used in almost every single Rust |
11 | //! program. |
12 | //! |
13 | //! # Other preludes |
14 | //! |
15 | //! Preludes can be seen as a pattern to make using multiple types more |
16 | //! convenient. As such, you'll find other preludes in the standard library, |
17 | //! such as [`std::io::prelude`]. Various libraries in the Rust ecosystem may |
18 | //! also define their own preludes. |
19 | //! |
20 | //! [`std::io::prelude`]: crate::io::prelude |
21 | //! |
22 | //! The difference between 'the prelude' and these other preludes is that they |
23 | //! are not automatically `use`'d, and must be imported manually. This is still |
24 | //! easier than importing all of their constituent components. |
25 | //! |
26 | //! # Prelude contents |
27 | //! |
28 | //! The first version of the prelude is used in Rust 2015 and Rust 2018, |
29 | //! and lives in [`std::prelude::v1`]. |
30 | //! [`std::prelude::rust_2015`] and [`std::prelude::rust_2018`] re-export this prelude. |
31 | //! It re-exports the following: |
32 | //! |
33 | //! * <code>[std::marker]::{[Copy], [Send], [Sized], [Sync], [Unpin]}</code>, |
34 | //! marker traits that indicate fundamental properties of types. |
35 | //! * <code>[std::ops]::{[Drop], [Fn], [FnMut], [FnOnce]}</code>, various |
36 | //! operations for both destructors and overloading `()`. |
37 | //! * <code>[std::mem]::[drop]</code>, a convenience function for explicitly |
38 | //! dropping a value. |
39 | //! * <code>[std::boxed]::[Box]</code>, a way to allocate values on the heap. |
40 | //! * <code>[std::borrow]::[ToOwned]</code>, the conversion trait that defines |
41 | //! [`to_owned`], the generic method for creating an owned type from a |
42 | //! borrowed type. |
43 | //! * <code>[std::clone]::[Clone]</code>, the ubiquitous trait that defines |
44 | //! [`clone`][Clone::clone], the method for producing a copy of a value. |
45 | //! * <code>[std::cmp]::{[PartialEq], [PartialOrd], [Eq], [Ord]}</code>, the |
46 | //! comparison traits, which implement the comparison operators and are often |
47 | //! seen in trait bounds. |
48 | //! * <code>[std::convert]::{[AsRef], [AsMut], [Into], [From]}</code>, generic |
49 | //! conversions, used by savvy API authors to create overloaded methods. |
50 | //! * <code>[std::default]::[Default]</code>, types that have default values. |
51 | //! * <code>[std::iter]::{[Iterator], [Extend], [IntoIterator], [DoubleEndedIterator], [ExactSizeIterator]}</code>, |
52 | //! iterators of various |
53 | //! kinds. |
54 | //! * <code>[std::option]::[Option]::{[self][Option], [Some], [None]}</code>, a |
55 | //! type which expresses the presence or absence of a value. This type is so |
56 | //! commonly used, its variants are also exported. |
57 | //! * <code>[std::result]::[Result]::{[self][Result], [Ok], [Err]}</code>, a type |
58 | //! for functions that may succeed or fail. Like [`Option`], its variants are |
59 | //! exported as well. |
60 | //! * <code>[std::string]::{[String], [ToString]}</code>, heap-allocated strings. |
61 | //! * <code>[std::vec]::[Vec]</code>, a growable, heap-allocated vector. |
62 | //! |
63 | //! The prelude used in Rust 2021, [`std::prelude::rust_2021`], includes all of the above, |
64 | //! and in addition re-exports: |
65 | //! |
66 | //! * <code>[std::convert]::{[TryFrom], [TryInto]}</code>, |
67 | //! * <code>[std::iter]::[FromIterator]</code>. |
68 | //! |
69 | //! [std::borrow]: crate::borrow |
70 | //! [std::boxed]: crate::boxed |
71 | //! [std::clone]: crate::clone |
72 | //! [std::cmp]: crate::cmp |
73 | //! [std::convert]: crate::convert |
74 | //! [std::default]: crate::default |
75 | //! [std::iter]: crate::iter |
76 | //! [std::marker]: crate::marker |
77 | //! [std::mem]: crate::mem |
78 | //! [std::ops]: crate::ops |
79 | //! [std::option]: crate::option |
80 | //! [`std::prelude::v1`]: v1 |
81 | //! [`std::prelude::rust_2015`]: rust_2015 |
82 | //! [`std::prelude::rust_2018`]: rust_2018 |
83 | //! [`std::prelude::rust_2021`]: rust_2021 |
84 | //! [std::result]: crate::result |
85 | //! [std::slice]: crate::slice |
86 | //! [std::string]: crate::string |
87 | //! [std::vec]: mod@crate::vec |
88 | //! [`to_owned`]: crate::borrow::ToOwned::to_owned |
89 | //! [book-closures]: ../../book/ch13-01-closures.html |
90 | //! [book-dtor]: ../../book/ch15-03-drop.html |
91 | //! [book-enums]: ../../book/ch06-01-defining-an-enum.html |
92 | //! [book-iter]: ../../book/ch13-02-iterators.html |
93 | |
94 | #![stable (feature = "rust1" , since = "1.0.0" )] |
95 | |
96 | mod common; |
97 | |
98 | /// The first version of the prelude of The Rust Standard Library. |
99 | /// |
100 | /// See the [module-level documentation](self) for more. |
101 | #[stable (feature = "rust1" , since = "1.0.0" )] |
102 | pub mod v1 { |
103 | #[stable (feature = "rust1" , since = "1.0.0" )] |
104 | pub use super::common::*; |
105 | |
106 | // Do not `doc(inline)` these `doc(hidden)` items. |
107 | #[unstable ( |
108 | feature = "rustc_encodable_decodable" , |
109 | issue = "none" , |
110 | soft, |
111 | reason = "derive macro for `rustc-serialize`; should not be used in new code" |
112 | )] |
113 | #[allow (deprecated)] |
114 | pub use core::prelude::v1::{RustcDecodable, RustcEncodable}; |
115 | } |
116 | |
117 | /// The 2015 version of the prelude of The Rust Standard Library. |
118 | /// |
119 | /// See the [module-level documentation](self) for more. |
120 | #[stable (feature = "prelude_2015" , since = "1.55.0" )] |
121 | pub mod rust_2015 { |
122 | #[stable (feature = "prelude_2015" , since = "1.55.0" )] |
123 | #[doc (no_inline)] |
124 | pub use super::v1::*; |
125 | } |
126 | |
127 | /// The 2018 version of the prelude of The Rust Standard Library. |
128 | /// |
129 | /// See the [module-level documentation](self) for more. |
130 | #[stable (feature = "prelude_2018" , since = "1.55.0" )] |
131 | pub mod rust_2018 { |
132 | #[stable (feature = "prelude_2018" , since = "1.55.0" )] |
133 | #[doc (no_inline)] |
134 | pub use super::v1::*; |
135 | } |
136 | |
137 | /// The 2021 version of the prelude of The Rust Standard Library. |
138 | /// |
139 | /// See the [module-level documentation](self) for more. |
140 | #[stable (feature = "prelude_2021" , since = "1.55.0" )] |
141 | pub mod rust_2021 { |
142 | #[stable (feature = "prelude_2021" , since = "1.55.0" )] |
143 | #[doc (no_inline)] |
144 | pub use super::v1::*; |
145 | |
146 | #[stable (feature = "prelude_2021" , since = "1.55.0" )] |
147 | #[doc (no_inline)] |
148 | pub use core::prelude::rust_2021::*; |
149 | } |
150 | |
151 | /// The 2024 version of the prelude of The Rust Standard Library. |
152 | /// |
153 | /// See the [module-level documentation](self) for more. |
154 | #[unstable (feature = "prelude_2024" , issue = "121042" )] |
155 | pub mod rust_2024 { |
156 | #[stable (feature = "rust1" , since = "1.0.0" )] |
157 | pub use super::common::*; |
158 | |
159 | #[unstable (feature = "prelude_2024" , issue = "121042" )] |
160 | #[doc (no_inline)] |
161 | pub use core::prelude::rust_2024::*; |
162 | } |
163 | |