1
1
# Crates and source files
2
2
3
+ r[ crate]
4
+
5
+ r[ crate.syntax]
3
6
> ** <sup >Syntax</sup >** \
4
7
> _ Crate_ :\
5
8
>   ;  ; [ _ InnerAttribute_ ] <sup >\* </sup >\
10
13
> compiler, and the language has always been designed to be compiled. For these
11
14
> reasons, this section assumes a compiler.
12
15
16
+ r[ crate.compile-time]
13
17
Rust's semantics obey a * phase distinction* between compile-time and
14
18
run-time.[ ^ phase-distinction ] Semantic rules that have a * static
15
19
interpretation* govern the success or failure of compilation, while
16
20
semantic rules that have a * dynamic interpretation* govern the behavior of the
17
21
program at run-time.
18
22
23
+ r[ crate.unit]
19
24
The compilation model centers on artifacts called _ crates_ . Each compilation
20
25
processes a single crate in source form, and if successful, produces a single
21
26
crate in binary form: either an executable or some sort of
22
27
library.[ ^ cratesourcefile ]
23
28
29
+ r[ crate.module]
24
30
A _ crate_ is a unit of compilation and linking, as well as versioning,
25
31
distribution, and runtime loading. A crate contains a _ tree_ of nested
26
32
[ module] scopes. The top level of this tree is a module that is
27
33
anonymous (from the point of view of paths within the module) and any item
28
34
within a crate has a canonical [ module path] denoting its location
29
35
within the crate's module tree.
30
36
37
+ r[ crate.input-source]
31
38
The Rust compiler is always invoked with a single source file as input, and
32
39
always produces a single output crate. The processing of that source file may
33
40
result in other source files being loaded as modules. Source files have the
34
41
extension ` .rs ` .
35
42
43
+ r[ crate.module-def]
36
44
A Rust source file describes a module, the name and location of which &mdash ;
37
45
in the module tree of the current crate &mdash ; are defined from outside the
38
46
source file: either by an explicit [ _ Module_ ] [ module ] item in a referencing
39
- source file, or by the name of the crate itself. Every source file is a
47
+ source file, or by the name of the crate itself.
48
+
49
+ r[ crate.inline-module]
50
+ Every source file is a
40
51
module, but not every module needs its own source file: [ module
41
52
definitions] [ module ] can be nested within one file.
42
53
54
+ r[ crate.items]
43
55
Each source file contains a sequence of zero or more [ _ Item_ ] definitions, and
44
56
may optionally begin with any number of [ attributes]
45
57
that apply to the containing module, most of which influence the behavior of
46
- the compiler. The anonymous crate module can have additional attributes that
58
+ the compiler.
59
+
60
+ r[ crate.attributes]
61
+ The anonymous crate module can have additional attributes that
47
62
apply to the crate as a whole.
48
63
49
64
> ** Note** : The file's contents may be preceded by a [ shebang] .
@@ -62,8 +77,13 @@ apply to the crate as a whole.
62
77
63
78
## Main Functions
64
79
65
- A crate that contains a ` main ` [ function] can be compiled to an executable. If a
66
- ` main ` function is present, it must take no arguments, must not declare any
80
+ r[ crate.main]
81
+
82
+ r[ crate.main.general]
83
+ A crate that contains a ` main ` [ function] can be compiled to an executable.
84
+
85
+ r[ crate.main.restriction]
86
+ If a ` main ` function is present, it must take no arguments, must not declare any
67
87
[ trait or lifetime bounds] , must not have any [ where clauses] , and its return
68
88
type must implement the [ ` Termination ` ] trait.
69
89
@@ -81,6 +101,7 @@ fn main() -> impl std::process::Termination {
81
101
}
82
102
```
83
103
104
+ r[ crate.main.import]
84
105
The ` main ` function may be an import, e.g. from an external crate or from the current one.
85
106
86
107
``` rust
@@ -105,19 +126,25 @@ use foo::bar as main;
105
126
106
127
### The ` no_main ` attribute
107
128
129
+ r[ crate.no_main]
130
+
108
131
The * ` no_main ` [ attribute] * may be applied at the crate level to disable
109
132
emitting the ` main ` symbol for an executable binary. This is useful when some
110
133
other object being linked to defines ` main ` .
111
134
112
135
## The ` crate_name ` attribute
113
136
137
+ r[ crate.crate_name]
138
+
139
+ r[ crate.crate_name.general]
114
140
The * ` crate_name ` [ attribute] * may be applied at the crate level to specify the
115
141
name of the crate with the [ _ MetaNameValueStr_ ] syntax.
116
142
117
143
``` rust
118
144
#![crate_name = " mycrate" ]
119
145
```
120
146
147
+ r[ crate.crate_name.restriction]
121
148
The crate name must not be empty, and must only contain [ Unicode alphanumeric]
122
149
or ` _ ` (U+005F) characters.
123
150
0 commit comments