Rust in the Browser
wasm-pack, wasm-bindgen, and getting a function to run on a web page.
cargo install wasm-pack
cargo new --lib life && cd life
cargo add wasm-bindgen
[lib]
crate-type = ["cdylib", "rlib"]
use wasm_bindgen::prelude::*;
#[wasm_bindgen]
pub fn greet(name: &str) -> String {
format!("Hello, {name}!")
}
wasm-pack build --target web
#[wasm_bindgen] generates the glue that converts between JS strings and Rust &str โ
which means copying across the boundary. Remember that; part 3 is about avoiding it.