20
20
Rust's semantics obey a * phase distinction* between compile-time and
21
21
run-time.[ ^ phase-distinction ] Semantic rules that have a * static
22
22
interpretation* govern the success or failure of compilation, while
23
- semantic rules
24
- that have a * dynamic interpretation* govern the behavior of the program at
25
- run-time.
23
+ semantic rules that have a * dynamic interpretation* govern the behavior of the
24
+ program at run-time.
26
25
27
26
The compilation model centers on artifacts called _ crates_ . Each compilation
28
27
processes a single crate in source form, and if successful, produces a single
@@ -66,22 +65,6 @@ apply to the crate as a whole.
66
65
#![warn(non_camel_case_types)]
67
66
```
68
67
69
- A crate that contains a ` main ` [ function] can be compiled to an executable. If a
70
- ` main ` function is present, it must take no arguments, must not declare any
71
- [ trait or lifetime bounds] , must not have any [ where clauses] , and its return
72
- type must be one of the following:
73
-
74
- * ` () `
75
- * ` Result<(), E> where E: Error `
76
- <!-- * `!` -->
77
- <!-- * Result<!, E> where E: Error` -->
78
-
79
- > Note: The implementation of which return types are allowed is determined by
80
- > the unstable [ ` Termination ` ] trait.
81
-
82
- <!-- If the previous section needs updating (from "must take no arguments"
83
- onwards, also update it in the attributes.md file, testing section -->
84
-
85
68
The optional [ _ UTF8 byte order mark_ ] (UTF8BOM production) indicates that the
86
69
file is encoded in UTF8. It can only occur at the beginning of the file and
87
70
is ignored by the compiler.
@@ -100,6 +83,48 @@ fn main() {
100
83
}
101
84
```
102
85
86
+ ## Preludes and ` no_std `
87
+
88
+ All crates have a * prelude* that automatically inserts names from a specific
89
+ module, the * prelude module* , into scope of each [ module] and an [ `extern
90
+ crate] ` into the crate root module. By default, the * standard prelude* is used.
91
+ The linked crate is [ ` std ` ] and the prelude module is [ ` std::prelude::v1 ` ] .
92
+
93
+ The prelude can be changed to the * core prelude* by using the ` no_std `
94
+ [ attribute] on the root crate module. The linked crate is [ ` core ` ] and the
95
+ prelude module is [ ` core::prelude::v1 ` ] . Using the core prelude over the
96
+ standard prelude is useful when either the crate is targeting a platform that
97
+ does not support the standard library or is purposefully not using the
98
+ capabilities of the standard library. Those capabilities are mainly dynamic
99
+ memory allocation (e.g. ` Box ` and ` Vec ` ) and file and network capabilities (e.g.
100
+ ` std::fs ` and ` std::io ` ).
101
+
102
+ <div class =" warning " >
103
+
104
+ Warning: Using ` no_std ` does not prevent the standard library from being linked
105
+ in. It is still valid to put ` extern crate std; ` into the crate and dependencies
106
+ can also link it in.
107
+
108
+ </div >
109
+
110
+ ## Main Functions
111
+
112
+ A crate that contains a ` main ` [ function] can be compiled to an executable. If a
113
+ ` main ` function is present, it must take no arguments, must not declare any
114
+ [ trait or lifetime bounds] , must not have any [ where clauses] , and its return
115
+ type must be one of the following:
116
+
117
+ * ` () `
118
+ * ` Result<(), E> where E: Error `
119
+ <!-- * `!` -->
120
+ <!-- * Result<!, E> where E: Error` -->
121
+
122
+ > Note: The implementation of which return types are allowed is determined by
123
+ > the unstable [ ` Termination ` ] trait.
124
+
125
+ <!-- If the previous section needs updating (from "must take no arguments"
126
+ onwards, also update it in the attributes.md file, testing section -->
127
+
103
128
[ ^ phase-distinction ] : This distinction would also exist in an interpreter.
104
129
Static checks like syntactic analysis, type checking, and lints should
105
130
happen before the program is executed regardless of when it is executed.
@@ -108,15 +133,21 @@ fn main() {
108
133
ECMA-335 CLI model, a * library* in the SML/NJ Compilation Manager, a * unit*
109
134
in the Owens and Flatt module system, or a * configuration* in Mesa.
110
135
111
- [ module ] : items/modules.html
112
- [ module path ] : paths.html
113
- [ attributes ] : attributes.html
114
- [ unit ] : types.html#tuple-types
115
136
[ _InnerAttribute_ ] : attributes.html
116
137
[ _Item_ ] : items.html
117
138
[ _shebang_ ] : https://en.wikipedia.org/wiki/Shebang_(Unix)
118
139
[ _utf8 byte order mark_ ] : https://en.wikipedia.org/wiki/Byte_order_mark#UTF-8
119
- [ function ] : items/functions.html
120
140
[ `Termination` ] : ../std/process/trait.Termination.html
121
- [ where clauses ] : items/generics.html#where-clauses
141
+ [ `core` ] : ../core/index.html
142
+ [ `core::prelude::v1` ] : ../core/preludce.index.html
143
+ [ `std` ] : ../std/index.html
144
+ [ `std::prelude::v1` ] : ../std/prelude/index.html
145
+ [ `use` declaration ] : items/use-declarations.html
146
+ [ attribute ] : attributes.html
147
+ [ attributes ] : attributes.html
148
+ [ function ] : items/functions.html
149
+ [ module ] : items/modules.html
150
+ [ module path ] : paths.html
122
151
[ trait or lifetime bounds ] : trait-bounds.html
152
+ [ unit ] : types.html#tuple-types
153
+ [ where clauses ] : items/generics.html#where-clauses
0 commit comments