A simple demo of compiling Rust code into wasm, and loading wasm in web page.
- rustc: 1.73.0
- target: wasm32-unknown-unknown
- Install rust from here: https://www.rust-lang.org/tools/install
- Install
wasm32-unknown-unknown
target with:rustup target add wasm32-unknown-unknown
- Compile rust code with:
cargo build --target wasm32-unknown-unknown --release
. A wasm file namedwasm_rust_demo.wasm
will be created under./target/wasm32-unknown-unknown/release/
directory. - Move
wasm_rust_demo.wasm
to root directory of this project. - Host
index.html
page with any server that you like. - In the browser console, try the function loaded from wasm file.
- WebAssembly is not yet integrated with
<script type='module'>
orimport
statements, thus there is not a path to have the browser fetch modules for you using imports. Link - WebAssembly has no direct access to the DOM apis. To access any Web API, WebAssembly needs to call out to JavaScript API, which then makes the Web API call. Link
- A proposal is made to support WebAssembly get direct access to Web APIs. Link