This demonstrates sharing memory between Rust and JavaScript using WASM.
The steps followed to create this were:
-
Create the project with
cargo new --lib wasm-rust-linear-memory
-
Modify
Cargo.toml
to contain:[lib] crate-type = ["cdylib"] [dependencies] wasm-bindgen = "0.2.70"
-
Modify
src/lib.rs
-
Build the project with
wasm-pack build --target web
-
Create
index.js
-
Create
index.html
To run this:
-
Start a local HTTP file server. One approach is to install Deno and then enter these commands:
deno install --allow-net --allow-read https://deno.land/std@0.87.0/http/file_server.ts file_server .
-
Browse localhost:{port} where port is the port on which the local server is listening.
-
See the output in the DevTools console.