| 1 |
# `main` |
| 2 |
|
| 3 |
When attached to the `main` function this attribute will adjust it to properly |
| 4 |
throw errors if they can be. |
| 5 |
|
| 6 |
```rust |
| 7 |
#[wasm_bindgen(main)] |
| 8 |
fn main() -> Result<(), JsValue> { |
| 9 |
Err(JsValue::from("this error message will be thrown")) |
| 10 |
} |
| 11 |
``` |
| 12 |
|
| 13 |
The attribute also allows using `async fn main()` in Cargo binaries. |
| 14 |
|
| 15 |
```rust |
| 16 |
#[wasm_bindgen(main)] |
| 17 |
async fn main() { |
| 18 |
// ... |
| 19 |
future.await; |
| 20 |
} |
| 21 |
``` |
| 22 |
|
| 23 |
This attribute is only intended to be used on the `main` function of binaries or |
| 24 |
examples. Unlike `#[wasm_bindgen(start)]`, it will not cause an arbitrary |
| 25 |
function to be executed on start in a library. |
| 26 |
|
| 27 |
The return type support is modeled after [`Termination`]. `()` and `Infallible` |
| 28 |
are supported, but [`Termination`] itself is not. In order, wasm-bindgen will |
| 29 |
first detect a `Result<(), impl Into<JsValue>>` and will throw proper |
| 30 |
`JsValue`s, `Result<(), impl Debug>` will convert an error to a string and throw |
| 31 |
that. |
| 32 |
|
| 33 |
[`Termination`]: https://doc.rust-lang.org/std/process/trait.Termination.html |