wasm-bindgen bridges the gap between Rust WebAssembly and JavaScript by generating typed glue code. Combined with wasm-pack, js_sys, and web_sys, it enables full access to the Web Platform from Rust.
0 / 5 completed
1 / 5
What is the primary purpose of wasm-bindgen?
wasm-bindgen is a Rust library and CLI tool that generates JS/TS bindings for Rust code compiled to WebAssembly. Without it, Wasm functions can only exchange numbers; wasm-bindgen adds support for strings, JS objects, closures, and DOM types by generating the glue code that handles serialisation across the boundary.
2 / 5
What does the #[wasm_bindgen] attribute do in Rust?
The #[wasm_bindgen] proc-macro attribute tells the wasm-bindgen toolchain to include the annotated item in the JS bindings. Applied to a pub fn, it exports the function to JavaScript. Applied to a pub struct, it makes the type available as a JS class with methods marked #[wasm_bindgen] becoming class methods.
3 / 5
What is JsValue in the wasm-bindgen API?
JsValue is wasm-bindgen's universal Rust handle for JavaScript values. It holds a reference into the JS heap via an index into an internal JS array. When a Rust function returns a JsValue or accepts one as an argument, wasm-bindgen manages the JS object's lifetime through a reference-counted slot in that array.
4 / 5
What is wasm-pack and how does it relate to wasm-bindgen?
wasm-pack is the recommended build tool for wasm-bindgen projects. Running wasm-pack build compiles the Rust crate with wasm32-unknown-unknown, invokes wasm-bindgen to generate the JS/TS glue, optimises the binary with wasm-opt, and produces an npm-compatible package in the pkg/ directory.
5 / 5
What does the web_sys crate provide in the wasm-bindgen ecosystem?
web_sys is a wasm-bindgen companion crate containing auto-generated bindings for the entire Web Platform API surface. Each Web API type and method is gated behind a Cargo feature flag (e.g., features = ["Window", "Document", "HtmlElement"]) so you only compile in the bindings you actually use, keeping binary size minimal.