11# Crates and source files
22
3+ r[ crate]
4+
5+ r[ crate.syntax]
36> ** <sup >Syntax</sup >** \
47> _ Crate_ :\
58>   ;  ; [ _ InnerAttribute_ ] <sup >\* </sup >\
1013> compiler, and the language has always been designed to be compiled. For these
1114> reasons, this section assumes a compiler.
1215
16+ r[ crate.compile-time]
1317Rust's semantics obey a * phase distinction* between compile-time and
1418run-time.[ ^ phase-distinction ] Semantic rules that have a * static
1519interpretation* govern the success or failure of compilation, while
1620semantic rules that have a * dynamic interpretation* govern the behavior of the
1721program at run-time.
1822
23+ r[ crate.unit]
1924The compilation model centers on artifacts called _ crates_ . Each compilation
2025processes a single crate in source form, and if successful, produces a single
2126crate in binary form: either an executable or some sort of
2227library.[ ^ cratesourcefile ]
2328
29+ r[ crate.module]
2430A _ crate_ is a unit of compilation and linking, as well as versioning,
2531distribution, and runtime loading. A crate contains a _ tree_ of nested
2632[ module] scopes. The top level of this tree is a module that is
2733anonymous (from the point of view of paths within the module) and any item
2834within a crate has a canonical [ module path] denoting its location
2935within the crate's module tree.
3036
37+ r[ crate.input-source]
3138The Rust compiler is always invoked with a single source file as input, and
3239always produces a single output crate. The processing of that source file may
3340result in other source files being loaded as modules. Source files have the
3441extension ` .rs ` .
3542
43+ r[ crate.module-def]
3644A Rust source file describes a module, the name and location of which &mdash ;
3745in the module tree of the current crate &mdash ; are defined from outside the
3846source 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
4051module, but not every module needs its own source file: [ module
4152definitions] [ module ] can be nested within one file.
4253
54+ r[ crate.items]
4355Each source file contains a sequence of zero or more [ _ Item_ ] definitions, and
4456may optionally begin with any number of [ attributes]
4557that 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
4762apply to the crate as a whole.
4863
4964> ** Note** : The file's contents may be preceded by a [ shebang] .
@@ -62,8 +77,13 @@ apply to the crate as a whole.
6277
6378## Main Functions
6479
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
6787[ trait or lifetime bounds] , must not have any [ where clauses] , and its return
6888type must implement the [ ` Termination ` ] trait.
6989
@@ -81,6 +101,7 @@ fn main() -> impl std::process::Termination {
81101}
82102```
83103
104+ r[ crate.main.import]
84105The ` main ` function may be an import, e.g. from an external crate or from the current one.
85106
86107``` rust
@@ -105,19 +126,26 @@ use foo::bar as main;
105126
106127### The ` no_main ` attribute
107128
129+ r[ crate.no_main]
130+
108131The * ` no_main ` [ attribute] * may be applied at the crate level to disable
109132emitting the ` main ` symbol for an executable binary. This is useful when some
110133other object being linked to defines ` main ` .
111134
112135## The ` crate_name ` attribute
113136
137+ r[ crate.crate_name]
138+
139+
140+ r[ crate.crate_name.general]
114141The * ` crate_name ` [ attribute] * may be applied at the crate level to specify the
115142name of the crate with the [ _ MetaNameValueStr_ ] syntax.
116143
117144``` rust
118145#![crate_name = " mycrate" ]
119146```
120147
148+ r[ crate.crate_name.restriction]
121149The crate name must not be empty, and must only contain [ Unicode alphanumeric]
122150or ` _ ` (U+005F) characters.
123151
0 commit comments