Skip to content

Commit 89c66ab

Browse files
committed
Document shebang restriction.
1 parent d17153c commit 89c66ab

File tree

1 file changed

+14
-1
lines changed

1 file changed

+14
-1
lines changed

src/crates-and-source-files.md

+14-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
1010
> **<sup>Lexer</sup>**\
1111
> UTF8BOM : `\uFEFF`\
12-
> SHEBANG : `#!` ~[`[` `\n`] ~`\n`<sup>\*</sup>
12+
> SHEBANG : `#!` \~`\n`<sup>\+</sup>[](#shebang)
1313
1414

1515
> Note: Although Rust, like any other language, can be implemented by an
@@ -65,10 +65,14 @@ apply to the crate as a whole.
6565
#![warn(non_camel_case_types)]
6666
```
6767

68+
## Byte order mark
69+
6870
The optional [_UTF8 byte order mark_] (UTF8BOM production) indicates that the
6971
file is encoded in UTF8. It can only occur at the beginning of the file and
7072
is ignored by the compiler.
7173

74+
## Shebang
75+
7276
A source file can have a [_shebang_] (SHEBANG production), which indicates
7377
to the operating system what program to use to execute this file. It serves
7478
essentially to treat the source file as an executable script. The shebang
@@ -84,6 +88,13 @@ fn main() {
8488
}
8589
```
8690

91+
There are two restrictions imposed on the shebang syntax to avoid confusion
92+
with an [attribute]. The shebang line must have at least one non-[whitespace]
93+
character anywhere after the `#!` characters. Additionally, The `#!`
94+
characters must not be followed by a `[` token, ignoring intervening
95+
[comments] or [whitespace]. If either of these restrictions fails, then it is
96+
not treated as a shebang, but instead as the start of an attribute.
97+
8798
## Preludes and `no_std`
8899

89100
All crates have a *prelude* that automatically inserts names from a specific
@@ -166,8 +177,10 @@ or `-` (U+002D) characters.
166177
[`std::prelude::v1`]: ../std/prelude/index.html
167178
[attribute]: attributes.md
168179
[attributes]: attributes.md
180+
[comments]: comments.md
169181
[function]: items/functions.md
170182
[module]: items/modules.md
171183
[module path]: paths.md
172184
[trait or lifetime bounds]: trait-bounds.md
173185
[where clauses]: items/generics.md#where-clauses
186+
[whitespace]: whitespace.md

0 commit comments

Comments
 (0)