wasm-bindgen for Rust to JavaScript interop in WebAssembly

Marcus Chen Jan 2026
1 tab
use wasm_bindgen::prelude::*;

#[wasm_bindgen]
pub fn greet(name: &str) -> String {
    format!("Hello, {}!", name)
}

#[wasm_bindgen]
pub fn add(a: i32, b: i32) -> i32 {
    a + b
}
1 file · rust Explain with highlit

Wasm-bindgen generates JavaScript bindings for Rust code compiled to WebAssembly. Annotate functions with #[wasm_bindgen], and the tool generates JS glue code. I use it to write performance-critical browser code in Rust: parsers, crypto, data processing. Rust types like String and Vec are automatically converted to JS equivalents. For complex types, implement JsValue conversions. The web-sys crate provides Rust bindings to Web APIs (DOM, fetch, etc.). Combined with wasm-pack, you can publish Rust libraries to npm. WebAssembly is a great way to bring Rust's safety and performance to the web.