View full source code or view the compiled example online
This is the “Hello, world!” example of #[wasm_bindgen] showing how to set up a project, export a function to JS, call it from JS, and then call the alert function in Rust.
Cargo.tomlThe Cargo.toml lists the wasm-bindgen crate as a dependency.
Also of note is the crate-type = ["cdylib"] which is largely used for wasm final artifacts today.
{{#include ../../../examples/hello_world/Cargo.toml}}
src/lib.rsHere we define our Rust entry point along with calling the alert function.
{{#include ../../../examples/hello_world/src/lib.rs}}
index.jsOur JS entry point is quite small!
{{#include ../../../examples/hello_world/index.js}}
Note: Webpack is required for this example, and if you‘re interested in options that don’t use a JS bundler see other examples.
And finally here's the Webpack configuration and package.json for this project:
webpack.config.js
{{#include ../../../examples/hello_world/webpack.config.js}}
package.json
{{#include ../../../examples/hello_world/package.json}}