Working with WebAssembly and Rust: From A to Z

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Introduction to WebAssembly and Rust

\n\n

WebAssembly (WASM) is a low-level binary instruction format that allows running high-performance code in the browser at near-native speed. Rust, with its ownership system and lack of garbage collector, is an ideal language for compiling to WebAssembly. In this article, we will go from installing tools to creating a full-fledged WASM module in Rust.

\n\n

Why Rust? It provides safe memory management, minimal binary size, and excellent performance. Combined with WebAssembly, you can write complex algorithms, games, graphics editors, and even full-fledged applications that run in the browser.

\n\n

Installing Tools and Setting Up the Environment

\n\n

To get started, you will need:

\n\n
    \n
  • Rust (installed via rustup)
  • \n
  • Target platform wasm32-unknown-unknown
  • \n
  • wasm-pack tool (for building and publishing WASM)
  • \n
  • A basic HTTP server (e.g., python3 -m http.server)
  • \n
\n\n

Installing the target platform and wasm-pack:

\n\n
# Install target platform for WebAssembly\nrustup target add wasm32-unknown-unknown\n\n# Install wasm-pack\ncargo install wasm-pack
\n\n

Now let's create a new project:

\n\n
cargo new --lib wasm-rust-demo\ncd wasm-rust-demo
\n\n

Open Cargo.toml and add the dependency:

\n\n
[lib]\ncrate-type = ["cdylib"]\n\n[dependencies]\nwasm-bindgen = "0.2"
\n\n

The cdylib flag indicates that we are creating a dynamic library compatible with WebAssembly.

\n\n

Writing Your First WASM Module in Rust

\n\n

Let's create a simple function that will be accessible from JavaScript. Edit src/lib.rs:

\n\n
use wasm_bindgen::prelude::*;\n\n#[wasm_bindgen]\npub fn greet(name: &str) -> String {\n    format!("Hello, {}! Welcome to the world of WebAssembly!", name)\n}\n\n#[wasm_bindgen]\npub fn fibonacci(n: u32) -> u32 {\n    match n {\n        0 => 0,\n        1 => 1,\n        _ => fibonacci(n - 1) + fibonacci(n - 2),\n    }\n}\n\n#[wasm_bindgen]\npub fn add(a: i32, b: i32) -> i32 {\n    a + b\n}
\n\n

The #[wasm_bindgen] attribute allows exporting functions to JavaScript. Note: String is automatically converted to a JavaScript string.

\n\n

Now let's build the project:

\n\n
wasm-pack build --target web
\n\n

After building, the following files will appear in the pkg folder:

\n\n
    \n
  • wasm_rust_demo_bg.wasm — compiled WASM module
  • \n
  • wasm_rust_demo.js — JavaScript wrapper for convenient import
  • \n
  • wasm_rust_demo.d.ts — TypeScript declarations
  • \n
\n\n

Integration with JavaScript and HTML

\n\n

Create an index.html file in the project root:

\n\n
<!DOCTYPE html>\n<html lang="en">\n<head>\n    <meta charset="UTF-8">\n    <meta name="viewport" content="width=device-width, initial-scale=1.0">\n    <title>Rust + WebAssembly Demo</title>\n</head>\n<body>\n    <h1>Rust + WebAssembly Example</h1>\n    \n    <div>\n        <h3>greet function</h3>\n        <input type="text" id="nameInput" placeholder="Enter name" value="World">\n        <button id="greetBtn">Greet</button>\n        <p id="greetOutput"></p>\n    </div>\n\n    <div>\n        <h3>Fibonacci Numbers</h3>\n        <input type="number" id="fibInput" value="10" min="0" max="40">\n        <button id="fibBtn">Calculate</button>\n        <p id="fibOutput"></p>\n    </div>\n\n    <script type="module">\n        import init, { greet, fibonacci, add } from './pkg/                

Blogs

Book Recommendations