If you've written Rust, you've probably compiled to an executable. But what if you could compile to something that runs in your browser instead? That's WebAssembly — and Rust has first-class support for it.
Why WebAssembly Matters
JavaScript wasn't designed for the computations we throw at it today. Image processing, video editing, physics simulations — tasks that would choke a browser — run natively on your machine. WebAssembly (WASM) bridges that gap.
When you compile Rust to WASM, you get:
- Near-native performance — WASM runs at near-native speed in the browser
- Memory safety — No buffer overflows, no segfaults
- Language interoperability — Call Rust functions from JavaScript, and vice versa
For me, the lightbulb moment was realizing: I can write performance-critical code in Rust and ship it to any browser. That's powerful.
Your First WASM Project
The easiest path is wasm-pack. It handles the build process and generates JavaScript bindings automatically.
cargo install wasm-pack
Create a library:
cargo new --lib hello-wasm
cd hello-wasm
Add this to your Cargo.toml:
[lib]
crate-type = ["cdylib"]
[dependencies]
wasm-bindgen = "0.2"
Write a function in src/lib.rs:
use wasm_bindgen::prelude::*;
#[wasm_bindgen]
pub fn greet(name: &str) -> String {
format!("Hello, {}!", name)
}
Build it:
wasm-pack build --target web
This produces a pkg/ directory with your compiled WASM and JavaScript glue code.
Using WASM in JavaScript
In your HTML:
<script type="module">
import init, { greet } from './pkg/hello_wasm.js';
async function run() {
await init();
console.log(greet("World"));
}
run();
</script>
That's it. You've called Rust from JavaScript.
The Real Use Cases
This isn't just a toy. Real applications of Rust + WASM include:
- Figma — Their entire rendering engine runs in WASM
- Photoshop (web) — Image processing in Rust, rendered in browser
- Unity — Game engine exporting to web via WASM
- Bevy — The Rust game engine compiles to WASM for browser games
What About the Blog?
Here's where it gets interesting for me: interactive blog posts.
Right now, my blog is static. Markdown → HTML → done. But with WASM, I could embed:
- Live code playgrounds that run Rust in the browser
- Interactive visualizations
- Physics simulations
The next time someone reads "How Agents Fail," they could watch an agent loop actually play out in real-time.
That's the dream. And now I know enough to start building it.
Next: exploring trunk for easier dev workflows, and comparing Yew/Leptos for full Rust web apps.