Skip to content
Open
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions src/SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

- [Lexical structure](lexical-structure.md)
- [Input format](input-format.md)
- [Shebang](shebang.md)
- [Keywords](keywords.md)
- [Identifiers](identifiers.md)
- [Comments](comments.md)
Expand Down
2 changes: 1 addition & 1 deletion src/crates-and-source-files.md
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,6 @@ The crate name must not be empty, and must only contain [Unicode alphanumeric] o
[module]: items/modules.md
[module path]: paths.md
[panic-docs]: panic.md#unwinding-across-ffi-boundaries
[shebang]: input-format.md#shebang-removal
[shebang]: shebang.md
[trait or lifetime bounds]: trait-bounds.md
[where clauses]: items/generics.md#where-clauses
31 changes: 2 additions & 29 deletions src/input-format.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,33 +41,8 @@ Other occurrences of the character `U+000D` (CR) are left in place (they are tre
r[input.shebang]
## Shebang removal

r[input.shebang.intro]
A *[shebang]* is an optional line that is typically used in Unix-like systems to specify an interpreter for executing the file.

> [!EXAMPLE]
> <!-- ignore: tests don't like shebang -->
> ```rust,ignore
> #!/usr/bin/env rustx
>
> fn main() {
> println!("Hello!");
> }
> ```

r[input.shebang.syntax]
```grammar,lexer
@root SHEBANG ->
`#!` !((WHITESPACE | LINE_COMMENT | BLOCK_COMMENT)* `[`)
~LF* (LF | EOF)
```

The shebang starts with the characters `#!` and extends through the first `U+000A` (LF) or through EOF if no LF is present. If the `#!` characters are followed by `[` (ignoring any intervening [comments] or [whitespace]), the line is not considered a shebang (to avoid ambiguity with an [inner attribute]).

r[input.shebang.position]
The shebang may appear immediately at the start of the file or after the optional [byte order mark].

r[input.shebang.removal]
The shebang is removed from the input sequence (and is therefore ignored).
If a [shebang] is present, it is removed from the input sequence (and is therefore ignored).

r[input.tokenization]
## Tokenization
Expand All @@ -83,9 +58,7 @@ The resulting sequence of characters is then converted into tokens as described
>
> The [`include_str!`] and [`include_bytes!`] macros do not apply these transformations.

[inner attribute]: attributes.md
[BYTE ORDER MARK]: https://en.wikipedia.org/wiki/Byte_order_mark#UTF-8
[comments]: comments.md
[Crates and source files]: crates-and-source-files.md
[shebang]: https://en.wikipedia.org/wiki/Shebang_(Unix)
[shebang]: shebang.md
[whitespace]: whitespace.md
34 changes: 34 additions & 0 deletions src/shebang.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
r[shebang]
# Shebang

r[shebang.syntax]
```grammar,lexer
@root SHEBANG ->
`#!` !((WHITESPACE | LINE_COMMENT | BLOCK_COMMENT)* `[`)
~LF* (LF | EOF)
```

r[shebang.intro]
Copy link
Contributor

Choose a reason for hiding this comment

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

Is there a reason the intro was moved to be below the syntax?

Generally I'd like to move to an outline where sections are generally organized so they start with an introduction, then have the syntax, then key behaviors and rules, and then more minutia like restrictions.

I realize there are big sections of the reference that still have the syntax first, but I would prefer to move to this style.

Copy link
Contributor Author

@traviscross traviscross Mar 5, 2026

Choose a reason for hiding this comment

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

No reason other than, as you say, matching similar nearby chapters. I had similarly considered it both ways. I'll move it.

A *[shebang]* is an optional line that is typically used in Unix-like systems to specify an interpreter for executing the file.

> [!EXAMPLE]
> <!-- ignore: tests don't like shebang -->
> ```rust,ignore
> #!/usr/bin/env rustx
>
> fn main() {
> println!("Hello!");
> }
> ```

r[shebang.syntax-description]
The shebang starts with the characters `#!` and extends through the first `U+000A` (LF) or through EOF if no LF is present. If the `#!` characters are followed by `[` (ignoring any intervening [comments] or [whitespace]), the line is not considered a shebang (to avoid ambiguity with an [inner attribute]).

r[shebang.position]
The shebang may appear immediately at the start of the file or after the optional [byte order mark].

[byte order mark]: https://en.wikipedia.org/wiki/Byte_order_mark#UTF-8
[comments]: comments.md
[inner attribute]: attributes.md
[shebang]: https://en.wikipedia.org/wiki/Shebang_(Unix)
[whitespace]: whitespace.md