| 1 |
# Hello, World! |
| 2 |
|
| 3 |
[View full source code][code] or [view the compiled example online][online] |
| 4 |
|
| 5 |
[online]: https://rustwasm.github.io/wasm-bindgen/exbuild/hello_world/ |
| 6 |
[code]: https://github.com/rustwasm/wasm-bindgen/tree/master/examples/hello_world |
| 7 |
|
| 8 |
This is the "Hello, world!" example of `#[wasm_bindgen]` showing how to set up |
| 9 |
a project, export a function to JS, call it from JS, and then call the `alert` |
| 10 |
function in Rust. |
| 11 |
|
| 12 |
## `Cargo.toml` |
| 13 |
|
| 14 |
The `Cargo.toml` lists the `wasm-bindgen` crate as a dependency. |
| 15 |
|
| 16 |
Also of note is the `crate-type = ["cdylib"]` which is largely used for wasm |
| 17 |
final artifacts today. |
| 18 |
|
| 19 |
```toml |
| 20 |
{{#include ../../../examples/hello_world/Cargo.toml}} |
| 21 |
``` |
| 22 |
|
| 23 |
## `src/lib.rs` |
| 24 |
|
| 25 |
Here we define our Rust entry point along with calling the `alert` function. |
| 26 |
|
| 27 |
```rust |
| 28 |
{{#include ../../../examples/hello_world/src/lib.rs}} |
| 29 |
``` |
| 30 |
|
| 31 |
## `index.js` |
| 32 |
|
| 33 |
Our JS entry point is quite small! |
| 34 |
|
| 35 |
```js |
| 36 |
{{#include ../../../examples/hello_world/index.js}} |
| 37 |
``` |
| 38 |
|
| 39 |
## Webpack-specific files |
| 40 |
|
| 41 |
> **Note**: Webpack is required for this example, and if you're interested |
| 42 |
> in options that don't use a JS bundler [see other examples][wab]. |
| 43 |
|
| 44 |
[wab]: without-a-bundler.html |
| 45 |
|
| 46 |
And finally here's the Webpack configuration and `package.json` for this |
| 47 |
project: |
| 48 |
|
| 49 |
**webpack.config.js** |
| 50 |
|
| 51 |
```js |
| 52 |
{{#include ../../../examples/hello_world/webpack.config.js}} |
| 53 |
``` |
| 54 |
|
| 55 |
**package.json** |
| 56 |
|
| 57 |
```json |
| 58 |
{{#include ../../../examples/hello_world/package.json}} |
| 59 |
``` |