Skip to content

Document restrictions of the main function #345

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 24, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 21 additions & 9 deletions src/crates-and-source-files.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@
> UTF8BOM : `\uFEFF`
> SHEBANG : `#!` ~[`[` `\n`] ~`\n`<sup>*</sup>

Although Rust, like any other language, can be implemented by an interpreter as
well as a compiler, the only existing implementation is a compiler,
and the language has
always been designed to be compiled. For these reasons, this section assumes a
compiler.

> Note: Although Rust, like any other language, can be implemented by an
> interpreter as well as a compiler, the only existing implementation is a
> compiler,and the language has always been designed to be compiled. For these
> reasons, this section assumes a compiler.

Rust's semantics obey a *phase distinction* between compile-time and
run-time.[^phase-distinction] Semantic rules that have a *static
Expand Down Expand Up @@ -66,9 +66,17 @@ apply to the crate as a whole.
#![warn(non_camel_case_types)]
```

A crate that contains a `main` function can be compiled to an executable. If a
`main` function is present, its return type must be `()`
("[unit]") and it must take no arguments.
A crate that contains a `main` [function] can be compiled to an executable. If a
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not from this PR, but binary crates are determined by the crate type, not the presence of main.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's a lot on that page I don't like, but I wanted to keep this change self-contained.

`main` function is present, it must take no arguments, must not declare any
[trait or lifetime bounds], must not have any [where clauses], and its return
type must be one of the following:

* `()`
<!-- * `!` -->
* `Result<T, E> where T: on this list, E: Error`

> Note: The implementation of which return types are allowed is determined by
> the unstable [`Termination`] trait.

The optional [_UTF8 byte order mark_] (UTF8BOM production) indicates that the
file is encoded in UTF8. It can only occur at the beginning of the file and
Expand Down Expand Up @@ -98,9 +106,13 @@ fn main() {

[module]: items/modules.html
[module path]: paths.html
[attributes]: items-and-attributes.html
[attributes]: attributes.html
[unit]: types.html#tuple-types
[_InnerAttribute_]: attributes.html
[_Item_]: items.html
[_shebang_]: https://en.wikipedia.org/wiki/Shebang_(Unix)
[_utf8 byte order mark_]: https://en.wikipedia.org/wiki/Byte_order_mark#UTF-8
[function]: items/functions.html
[`Termination`]: ../std/process/trait.Termination.html
[where clause]: items/where-clauses.html
[trait or lifetime bounds]: trait-bounds.html