# Rust and WebAssembly in 2026: Wasm, wasm-bindgen and Interview Questions > Complete guide to Rust WebAssembly development with wasm-pack and wasm-bindgen. Learn to build, optimize, and test Wasm modules with practical examples and interview questions. - Published: 2026-08-07 - Updated: 2026-08-07 - Author: SharpSkill - Tags: rust, webassembly, wasm, wasm-bindgen, tutorial - Reading time: 12 min --- Rust WebAssembly (Wasm) brings near-native performance to web applications by compiling Rust code to a binary format that runs in browsers and server environments. The combination of Rust's memory safety guarantees with WebAssembly's sandboxed execution creates a powerful foundation for performance-critical web components, from image processing to cryptographic operations. > **Key Takeaway** > > Rust compiles to WebAssembly through `wasm-pack` and `wasm-bindgen`, producing modules that JavaScript can import and call directly. The workflow involves writing Rust functions, annotating them with `#[wasm_bindgen]`, compiling to `.wasm`, and importing the generated JavaScript glue code into any web application. ## Setting Up Rust for WebAssembly Development The Rust toolchain requires the `wasm32-unknown-unknown` target and `wasm-pack` for building WebAssembly modules. These tools handle compilation, optimization, and JavaScript binding generation automatically. ```bash # install-wasm-tools.sh # Add the WebAssembly target to rustup rustup target add wasm32-unknown-unknown # Install wasm-pack for building and packaging cargo install wasm-pack # Verify installation wasm-pack --version ``` After installation, create a new library crate configured for WebAssembly output. The `Cargo.toml` file requires specific settings for Wasm compatibility. ```toml # Cargo.toml [package] name = "wasm-calculator" version = "0.1.0" edition = "2024" [lib] crate-type = ["cdylib", "rlib"] [dependencies] wasm-bindgen = "0.2.100" [profile.release] opt-level = "z" # Optimize for size lto = true # Link-time optimization ``` The `cdylib` crate type produces a dynamic library suitable for WebAssembly. The release profile optimizations reduce the final `.wasm` file size significantly—often by 50% or more compared to unoptimized builds. ## Understanding wasm-bindgen and JavaScript Interop The [wasm-bindgen](https://rustwasm.github.io/docs/wasm-bindgen/) crate bridges Rust and JavaScript by generating type-safe bindings automatically. Functions annotated with `#[wasm_bindgen]` become callable from JavaScript, while JavaScript functions can be imported into Rust. ```rust // src/lib.rs use wasm_bindgen::prelude::*; // Export this function to JavaScript #[wasm_bindgen] pub fn fibonacci(n: u32) -> u32 { // Base cases for recursion match n { 0 => 0, 1 => 1, // Recursive calculation with tail-call optimization _ => fibonacci(n - 1) + fibonacci(n - 2), } } // Import console.log from JavaScript #[wasm_bindgen] extern "C" { #[wasm_bindgen(js_namespace = console)] fn log(s: &str); } // Export a function that uses console.log #[wasm_bindgen] pub fn greet(name: &str) { log(&format!("Hello, {}!", name)); } ``` The `extern "C"` block declares JavaScript functions that Rust code can call. The `js_namespace` attribute specifies the JavaScript object containing the function. This bidirectional binding enables seamless integration between Rust logic and JavaScript APIs. ## Building and Bundling WebAssembly Modules The `wasm-pack build` command compiles Rust to WebAssembly and generates JavaScript wrapper code. Different targets suit different JavaScript environments. ```bash # build-wasm.sh # Build for bundlers like webpack, Vite, or Rollup wasm-pack build --target bundler --release # Build for native ES modules (browser) wasm-pack build --target web --release # Build for Node.js wasm-pack build --target nodejs --release ``` Each target produces different output in the `pkg/` directory. The bundler target works with modern JavaScript bundlers that support ES modules and WebAssembly imports. The web target produces code that loads directly in browsers via `