wasm-bindgen interoperability
Quick answer
wasm-bindgen is a Rust crate and companion tool for generating bindings between Wasm and JavaScript. It handles richer interfaces than numeric raw Wasm exports, including string conversion.
Define a small binding surface
Use the wasm_bindgen attribute on functions intended for JavaScript. The generated JavaScript glue initializes the module and converts supported values; strings cross an encoding and memory boundary. Keep expensive loops inside the module and exchange data in meaningful batches. Use matching candidate crate and CLI versions 0.2.105 from V119; resolve and review the lockfile first. The Rust snippet belongs in a cdylib project; the terminal example assumes a compatible, already installed CLI.
use wasm_bindgen::prelude::*;#[wasm_bindgen]pub fn greet(name: &str) -> String { format!("Hello, {name}!") }Manual local Rust/Cargo candidate; not executed. Expected behavior:
After generating and initializing matching JS bindings, greet("Ada") returns Hello, Ada!.The bridge converts text; a borrowed Rust str does not become a permanently shared JavaScript reference.
wasm-bindgen --versioncargo +1.85.0 build --locked --release --target wasm32-unknown-unknownwasm-bindgen --target web --out-dir pkg target/wasm32-unknown-unknown/release/greeting.wasmManual terminal step; not executed. Expected behavior (environment dependent):
Generated JavaScript glue and Wasm file if the cdylib, dependency, target and matching CLI are configured.Serve the generated files through an appropriate local development server; initialize using the generated module's documented API.
Common mistake
Do not call generated exports before initialization or assume string crossing is free.
Try it yourself
Design a batch API for normalizing 100 labels; identify how to verify UTF-8 and conversion behavior.
Static examples for Rust 1.85.0, edition 2024. Run them locally in a disposable project; ByteMind execution support and these outputs have not been verified.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
wasm-bindgen interoperability — practice quiz
Choose one answer per question. Code examples are predictions; review explanations after attempting them.
A generated JS binding is called before the Wasm module is initialized. What is missing?
wasm-bindgen interoperability — example 1. Read the code; do not execute it for this question. Assume the lesson’s stated setup, compatible dependencies and toolchain, and a fresh disposable directory for file-creating examples. ```rust use wasm_bindgen::prelude::*; #[wasm_bindgen] pub fn greet(name: &str) -> String { format!("Hello, {name}!") } ``` Which outcome or interpretation best matches this example?
wasm-bindgen interoperability — example 2. Read the code; do not execute it for this question. Assume the lesson’s stated setup, compatible dependencies and toolchain, and a fresh disposable directory for file-creating examples. ```bash wasm-bindgen --version cargo +1.85.0 build --locked --release --target wasm32-unknown-unknown wasm-bindgen --target web --out-dir pkg target/wasm32-unknown-unknown/release/greeting.wasm ``` Which outcome or interpretation best matches this example?