summaryrefslogtreecommitdiffstats
path: root/src/doc/unstable-book/src/language-features/start.md
blob: 09e4875a2e4f7c1860a9fd767b282487df57f1fd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# `start`

The tracking issue for this feature is: [#29633]

[#29633]: https://github.com/rust-lang/rust/issues/29633

------------------------

Allows you to mark a function as the entry point of the executable, which is
necessary in `#![no_std]` environments.

The function marked `#[start]` is passed the command line parameters in the same
format as the C main function (aside from the integer types being used).
It has to be non-generic and have the following signature:

```rust,ignore (only-for-syntax-highlight)
# let _:
fn(isize, *const *const u8) -> isize
# ;
```

This feature should not be confused with the `start` *lang item* which is
defined by the `std` crate and is written `#[lang = "start"]`.

## Usage together with the `std` crate

`#[start]` can be used in combination with the `std` crate, in which case the
normal `main` function (which would get called from the `std` crate) won't be
used as an entry point.
The initialization code in `std` will be skipped this way.

Example:

```rust
#![feature(start)]

#[start]
fn start(_argc: isize, _argv: *const *const u8) -> isize {
    0
}
```

Unwinding the stack past the `#[start]` function is currently considered
Undefined Behavior (for any unwinding implementation):

```rust,ignore (UB)
#![feature(start)]

#[start]
fn start(_argc: isize, _argv: *const *const u8) -> isize {
    std::panic::catch_unwind(|| {
        panic!(); // panic safely gets caught or safely aborts execution
    });

    panic!(); // UB!

    0
}
```