]> git.proxmox.com Git - rustc.git/blame - src/doc/reference/src/crates-and-source-files.md
New upstream version 1.65.0+dfsg1
[rustc.git] / src / doc / reference / src / crates-and-source-files.md
CommitLineData
8bb4bdeb
XL
1# Crates and source files
2
8faf50e0
XL
3> **<sup>Syntax</sup>**\
4> _Crate_ :\
5> &nbsp;&nbsp; UTF8BOM<sup>?</sup>\
6> &nbsp;&nbsp; SHEBANG<sup>?</sup>\
7> &nbsp;&nbsp; [_InnerAttribute_]<sup>\*</sup>\
8> &nbsp;&nbsp; [_Item_]<sup>\*</sup>
9
10> **<sup>Lexer</sup>**\
11> UTF8BOM : `\uFEFF`\
3dfed10e 12> SHEBANG : `#!` \~`\n`<sup>\+</sup>[†](#shebang)
3b2f2976 13
94b46f34
XL
14
15> Note: Although Rust, like any other language, can be implemented by an
16> interpreter as well as a compiler, the only existing implementation is a
13cf67c4 17> compiler, and the language has always been designed to be compiled. For these
94b46f34 18> reasons, this section assumes a compiler.
8bb4bdeb
XL
19
20Rust's semantics obey a *phase distinction* between compile-time and
21run-time.[^phase-distinction] Semantic rules that have a *static
22interpretation* govern the success or failure of compilation, while
0bf4aa26
XL
23semantic rules that have a *dynamic interpretation* govern the behavior of the
24program at run-time.
8bb4bdeb
XL
25
26The compilation model centers on artifacts called _crates_. Each compilation
27processes a single crate in source form, and if successful, produces a single
28crate in binary form: either an executable or some sort of
29library.[^cratesourcefile]
30
31A _crate_ is a unit of compilation and linking, as well as versioning,
e1599b0c 32distribution, and runtime loading. A crate contains a _tree_ of nested
8bb4bdeb
XL
33[module] scopes. The top level of this tree is a module that is
34anonymous (from the point of view of paths within the module) and any item
35within a crate has a canonical [module path] denoting its location
36within the crate's module tree.
37
38The Rust compiler is always invoked with a single source file as input, and
39always produces a single output crate. The processing of that source file may
40result in other source files being loaded as modules. Source files have the
41extension `.rs`.
42
43A Rust source file describes a module, the name and location of which &mdash;
44in the module tree of the current crate &mdash; are defined from outside the
9fa01778 45source file: either by an explicit [_Module_][module] item in a referencing
13cf67c4
XL
46source file, or by the name of the crate itself. Every source file is a
47module, but not every module needs its own source file: [module
48definitions][module] can be nested within one file.
8bb4bdeb 49
9fa01778 50Each source file contains a sequence of zero or more [_Item_] definitions, and
8bb4bdeb
XL
51may optionally begin with any number of [attributes]
52that apply to the containing module, most of which influence the behavior of
53the compiler. The anonymous crate module can have additional attributes that
54apply to the crate as a whole.
55
60c5eb7d 56```rust
8bb4bdeb
XL
57// Specify the crate name.
58#![crate_name = "projx"]
59
60// Specify the type of output artifact.
61#![crate_type = "lib"]
62
63// Turn on a warning.
64// This can be done in any module, not just the anonymous crate module.
65#![warn(non_camel_case_types)]
66```
67
3dfed10e
XL
68## Byte order mark
69
3b2f2976
XL
70The optional [_UTF8 byte order mark_] (UTF8BOM production) indicates that the
71file is encoded in UTF8. It can only occur at the beginning of the file and
72is ignored by the compiler.
73
3dfed10e
XL
74## Shebang
75
3b2f2976
XL
76A source file can have a [_shebang_] (SHEBANG production), which indicates
77to the operating system what program to use to execute this file. It serves
78essentially to treat the source file as an executable script. The shebang
79can only occur at the beginning of the file (but after the optional
80_UTF8BOM_). It is ignored by the compiler. For example:
81
60c5eb7d
XL
82<!-- ignore: tests don't like shebang -->
83```rust,ignore
3b2f2976
XL
84#!/usr/bin/env rustx
85
86fn main() {
87 println!("Hello!");
88}
89```
90
3dfed10e
XL
91A restriction is imposed on the shebang syntax to avoid confusion with an
92[attribute]. The `#!` characters must not be followed by a `[` token, ignoring
93intervening [comments] or [whitespace]. If this restriction fails, then it is
94not treated as a shebang, but instead as the start of an attribute.
95
0bf4aa26
XL
96## Preludes and `no_std`
97
5869c6ff
XL
98This section has been moved to the [Preludes chapter](names/preludes.md).
99<!-- this is to appease the linkchecker, will remove once other books are updated -->
0bf4aa26
XL
100
101## Main Functions
102
103A crate that contains a `main` [function] can be compiled to an executable. If a
104`main` function is present, it must take no arguments, must not declare any
105[trait or lifetime bounds], must not have any [where clauses], and its return
04454e1e 106type must implement the [`Termination`] trait.
0bf4aa26 107
04454e1e
FG
108```rust
109fn main() {}
110```
111```rust
112fn main() -> ! {
113 std::process::exit(0);
114}
115```
116```rust
117fn main() -> impl std::process::Termination {
118 std::process::ExitCode::SUCCESS
119}
120```
0bf4aa26 121
04454e1e
FG
122> **Note**: Types with implementations of [`Termination`] in the standard library include:
123>
124> * `()`
125> * [`!`]
f2b60f7d 126> * [`Infallible`]
04454e1e 127> * [`ExitCode`]
f2b60f7d 128> * `Result<T, E> where T: Termination, E: Debug`
0bf4aa26
XL
129
130<!-- If the previous section needs updating (from "must take no arguments"
532ac7d7
XL
131 onwards, also update it in the testing.md file -->
132
133### The `no_main` attribute
134
135The *`no_main` [attribute]* may be applied at the crate level to disable
136emitting the `main` symbol for an executable binary. This is useful when some
137other object being linked to defines `main`.
138
139## The `crate_name` attribute
140
141The *`crate_name` [attribute]* may be applied at the crate level to specify the
142name of the crate with the [_MetaNameValueStr_] syntax.
143
60c5eb7d 144```rust
532ac7d7
XL
145#![crate_name = "mycrate"]
146```
147
148The crate name must not be empty, and must only contain [Unicode alphanumeric]
3c0e092e 149or `_` (U+005F) characters.
0bf4aa26 150
8bb4bdeb
XL
151[^phase-distinction]: This distinction would also exist in an interpreter.
152 Static checks like syntactic analysis, type checking, and lints should
153 happen before the program is executed regardless of when it is executed.
154
155[^cratesourcefile]: A crate is somewhat analogous to an *assembly* in the
156 ECMA-335 CLI model, a *library* in the SML/NJ Compilation Manager, a *unit*
157 in the Owens and Flatt module system, or a *configuration* in Mesa.
158
532ac7d7 159[Unicode alphanumeric]: ../std/primitive.char.html#method.is_alphanumeric
04454e1e 160[`!`]: types/never.md
416331ca
XL
161[_InnerAttribute_]: attributes.md
162[_Item_]: items.md
163[_MetaNameValueStr_]: attributes.md#meta-item-attribute-syntax
3b2f2976
XL
164[_shebang_]: https://en.wikipedia.org/wiki/Shebang_(Unix)
165[_utf8 byte order mark_]: https://en.wikipedia.org/wiki/Byte_order_mark#UTF-8
04454e1e 166[`ExitCode`]: ../std/process/struct.ExitCode.html
f2b60f7d 167[`Infallible`]: ../std/convert/enum.Infallible.html
94b46f34 168[`Termination`]: ../std/process/trait.Termination.html
416331ca
XL
169[attribute]: attributes.md
170[attributes]: attributes.md
3dfed10e 171[comments]: comments.md
416331ca
XL
172[function]: items/functions.md
173[module]: items/modules.md
174[module path]: paths.md
175[trait or lifetime bounds]: trait-bounds.md
176[where clauses]: items/generics.md#where-clauses
3dfed10e 177[whitespace]: whitespace.md
5869c6ff
XL
178
179<script>
180(function() {
181 var fragments = {
182 "#preludes-and-no_std": "names/preludes.html",
183 };
184 var target = fragments[window.location.hash];
185 if (target) {
186 var url = window.location.toString();
187 var base = url.substring(0, url.lastIndexOf('/'));
188 window.location.replace(base + "/" + target);
189 }
190})();
191</script>